Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm using a part of this code to disable hotkeys.

But even after 2 days of lurking around the web, I haven't found a solution for the error 1401 which doesn't disable ALT+TAB, I would ignore this if I could and just put the line of code for ALT+TAB in comentary, but I realy need it to be disabled since I'm running a cybercafe based application.

Using:
C#
using System.Diagnostics; //para poder utilizar a CMD do computador
using System.Runtime.InteropServices; //para poder utilizar a CMD do computador


DLL Library:
C#
#region Dynamic Link Library Imports

[DllImport("user32.dll")]
private static extern int FindWindow(string cls, string wndwText);

[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int cmd);

[DllImport("user32.dll")]
private static extern long SHAppBarMessage(long dword, int cmd);

[DllImport("user32.dll")]
private static extern int RegisterHotKey(IntPtr hwnd, int id, int
fsModifiers, int vk);

[DllImport("user32.dll")]
private static extern int UnregisterHotKey(IntPtr hwnd, int id);

#endregion


Constants and Variables:
C#
#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


MainCode to block the hotkeys:
C#
#region


        private void buttonlock()
        {
            // Related browser window key combinations
            // -- Some things that you may want to disable --
            //CTRL+A           Select all
            //CTRL+B           Organize favorites
            //CTRL+C           Copy
            //CTRL+F           Find
            //CTRL+H           View history
            //CTRL+L           Open locate
            //CTRL+N           Open new browser window
            //CTRL+O           Open locate
            //CTRL+P           Print
            //CTRL+R           Refresh
            //CTRL+S           Save
            //CTRL+V           Paste
            //CTRL+W           Close
            //CTRL+X           Cut
            //ALT+F4           Close

            // Use CTRL+ALT+DEL to open the task manager,
            // kill Internet Explorer and then close the application window
            // to exit

            // 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);

            // Disable ALT+Tab - tab through open applications
            RegisterGlobalHotKey(Keys.Tab, USE_ALT);

            // hide the task bar - not a big deal, they can
            // still CTRL+ESC to get the start menu; for that
            // matter, CTRL+ALT+DEL also works; if you need to
            // disable that you will have to violate SAS and 
            // monkey with the security policies on the machine
            ShowWindow(FindWindow("Shell_TrayWnd", null), 0);  
        }



        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
                        MessageBox.Show("Error: " +
                            mHotKeyId.ToString() + " - " +
                            Marshal.GetLastWin32Error().ToString(),
                            "Hot Key Registration");
                    }
                }
            }
            catch
            {
                // clean up if hotkey registration failed -
                // nothing works if it fails
                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
            }
        }

#endregion
Posted
Comments
CHill60 12-Apr-13 13:19pm    
Use the Improve question link to add the version of Windows you are targetting - it is relevent

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900