School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
You're Browsing As A Guest! Register Now...
Become an Expert!

Join 353,811 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,420 people online right now.Registration is fast and FREE... Join Now!



disable ALT+F4 using c#

52 Weeks of Code Challenge: WPF
Week #10 of the 52 Weeks of Code Challenge is WPF. If you're a .NET programmer, you should give it a shot. Click Here!

disable ALT+F4 using c# Rate Topic: -----

#1 Mythili  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-April 08


Dream Kudos: 0

Posted 08 April 2008 - 10:24 PM

hi,

i'm working in asp.net with c#.

In my application, i want to disable ALT+F4 key so that user should not closed the form(webform)

can anyone send me the simple code for disabling ALT+F4 using c# code......................

thanks in advance!!!! :)
Was This Post Helpful? 0
  • +
  • -


#2 MRJ  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 4
  • View blog
  • Posts: 117
  • Joined: 13-October 07


Dream Kudos: 0

Re: disable ALT+F4 using c#

Posted 09 April 2008 - 12:18 PM

I don't think you'll get any code.

First this forum doesn't just procudce code to order. You have to try first, and post questions about where you are stuck. We don't do your work for you.

Second I'm pretty sure that it's imposible to disable Alt-F4 from a WebForm. It might be posible with a Windows form, but not a web form.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • apt-get install DIC.bin
  • Icon

Reputation: 701
  • View blog
  • Posts: 16,787
  • Joined: 26-July 07


Dream Kudos: 12450

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: disable ALT+F4 using c#

Posted 09 April 2008 - 01:00 PM

MRJ is correct. In ASP.Net any key strokes that are captured are what takes place on the server, not on the client. Without Impersonation, encryption algorithms and lots of code/security you're not going to gain access to key strokes of the client machine. Even with Impersonation you're going to have the permission of the user in order to be able to accomplish something like this.

What is your purpose for accomplishing this?
Was This Post Helpful? 0
  • +
  • -

#4 Mythili  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-April 08


Dream Kudos: 0

Re: disable ALT+F4 using c#

Posted 09 April 2008 - 09:46 PM

View PostPsychoCoder, on 9 Apr, 2008 - 02:00 PM, said:

MRJ is correct. In ASP.Net any key strokes that are captured are what takes place on the server, not on the client. Without Impersonation, encryption algorithms and lots of code/security you're not going to gain access to key strokes of the client machine. Even with Impersonation you're going to have the permission of the user in order to be able to accomplish something like this.

What is your purpose for accomplishing this?



hi all,
actually, we are doing security questions registration for the users who want to reset their password. The user should not skip this page while resetting their password. So i want to disable hot key combinations such as ALT+F4,ALT+TAB etc. i tried the follwoing code. it will throw two exceptions Error 1400 - INVALID_WINDOW_HANDLE, Error 1409 HOTKEY_ALREADY_REGISTERED.with those exceptions it disabled the hotkeys but i cant able to enable it again.

here is my code....

 #region Dynamic Link Library Imports
        [DllImport("user32.dll",SetLastError=true)]

        private static extern int FindWindow(string cls, string wndwText);

        [DllImport("user32.dll",SetLastError=true)]

        private static extern int ShowWindow(int hwnd, int cmd);

        [DllImport("user32.dll", SetLastError = true)]

        private static extern long SHAppBarMessage(long dword, int cmd);

        [DllImport("user32.dll", SetLastError = true)]

        private static extern int RegisterHotKey(IntPtr hwnd, int id, int

        fsModifiers, int vk);

        [DllImport("user32.dll", SetLastError = true)]

        private static extern int UnregisterHotKey(IntPtr hwnd, int id);
        #endregion


        #region Modifier Constants and Variables
        // Constants for modifier keys
        private const int USE_ALT = 1;
        private const int USE_CTRL = 2;
        private const int USE_SHIFT = 4;
        private const int USE_WIN = 8;

        // Hot key ID tracker
        short mHotKeyId = 0;
        #endregion


        public ki()
        {
         //InitializeComponent();

        }
        public void unreg()
        {
            UnregisterGlobalHotKey();
        }
        public void sampfn()
        {
            // Disable ALT+F4 - exit
            RegisterGlobalHotKey(Keys.F4, USE_ALT);

            // Disable CTRL+W - exit
              RegisterGlobalHotKey(Keys.W, USE_CTRL);

           // Disable CTRL+N - new window
            RegisterGlobalHotKey(Keys.N, USE_CTRL);

           // Disable CTRL+S - save
            RegisterGlobalHotKey(Keys.S, USE_CTRL);

            // Disable CTRL+A - select all
            RegisterGlobalHotKey(Keys.A, USE_CTRL);

            // Disable CTRL+C - copy
           RegisterGlobalHotKey(Keys.C, USE_CTRL);

            // Disable CTRL+X - cut
            RegisterGlobalHotKey(Keys.X, USE_CTRL);

            // Disable CTRL+V - paste
            RegisterGlobalHotKey(Keys.V, USE_CTRL);

            // Disable CTRL+B - organize favorites
            RegisterGlobalHotKey(Keys.B, USE_CTRL);

            // Disable CTRL+F - find
            RegisterGlobalHotKey(Keys.F, USE_CTRL);

            // Disable CTRL+H - view history
            RegisterGlobalHotKey(Keys.H, USE_CTRL);

        }



        private void RegisterGlobalHotKey(Keys hotkey, int modifiers)
        {
            try
            {
                // increment the hot key value - we are just identifying

                // them with a sequential number since we have multiples

                mHotKeyId++;
                if (mHotKeyId > 0)
                {
                    // register the hot key combination
                    if (RegisterHotKey(this.Handle, mHotKeyId, modifiers,
                    Convert.ToInt16(hotkey)) == 0)
                    {
                        // tell the user which combination failed to register 
                        // this is useful to you, not an end user; the user
                        // should never see this application run
                        UnregisterGlobalHotKey();
                        MessageBox.Show("Error: " + mHotKeyId.ToString() + " - " + Marshal.GetLastWin32Error().ToString(), "HotKey Registration");
                    }

                }

            }

            catch
           {

                // clean up if hotkey registration failed -

                // nothing works if it fails
                MessageBox.Show(e.ToString());
                UnregisterGlobalHotKey();

            }

        }

        public void fun()
        {
            UnregisterGlobalHotKey();
        }

        private void UnregisterGlobalHotKey()
        {

            // loop through each hotkey id and

            // disable it

            for (int i = 0; i < mHotKeyId; i++)
            {

                UnregisterHotKey(this.Handle, i);

            }
        }
   


        protected override void WndProc(ref Message m)
        {

            base.WndProc(ref m);


            // if the message matches,

            // disregard it

            const int WM_HOTKEY = 0x312;

            if (m.Msg == WM_HOTKEY)
            {

                // Ignore the request or each

                // disabled hotkey combination

            }

        }



        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {

            // unregister the hot keys

            UnregisterGlobalHotKey();

            // show the taskbar - does not matter really

            ShowWindow(FindWindow("Shell_TrayWnd", null), 1);

        }
    }
}




I am calling this sampfn() in Button_Click event..

we didnt post this thread without trying any thing...

we dont know how to solve this problem....

help us to solve this ......

This post has been edited by PsychoCoder: 10 April 2008 - 02:43 AM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month