12

Can anyone please tell me how to disable the task switch keys using c#

ASr..
  • 458
  • 2
  • 7
  • 21
  • 1
    Why would you want to do this? – Fosco Jul 09 '10 at 14:34
  • 4
    Any type of kiosk application... Valid question not sure why the DV, this is probably a duplicate though. – Chris Marisic Jul 09 '10 at 14:37
  • Most PC games do this. Exclusive fullscreen gets better performance on many systems, and who wants Skype chat popping up while you're trying to play your favourite game? – yoyo Feb 24 '16 at 17:45
  • Remote control programs trap alt-tab and windows-tab so they can pass those keystrokes to the remote computer. – SteveSobol Jul 10 '23 at 15:54

2 Answers2

25

I've got the complete code to disable Windows Key , Alt + Tab and so on..

And now I'm providing the following code as a reference for others:

    /* Code to Disable WinKey, Alt+Tab, Ctrl+Esc Starts Here */

    // Structure contain information about low-level keyboard input event 
    [StructLayout(LayoutKind.Sequential)]
    private struct KBDLLHOOKSTRUCT
    {
        public Keys key;
        public int scanCode;
        public int flags;
        public int time;
        public IntPtr extra;
    }
    //System level functions to be used for hook and unhook keyboard input  
    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool UnhookWindowsHookEx(IntPtr hook);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string name);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern short GetAsyncKeyState(Keys key);
    //Declaring Global objects     
    private IntPtr ptrHook;
    private LowLevelKeyboardProc objKeyboardProcess;

    private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
    {
        if (nCode >= 0)
        {
            KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));

            // Disabling Windows keys 

            if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Tab && HasAltModifier(objKeyInfo.flags) || objKeyInfo.key == Keys.Escape && (ModifierKeys & Keys.Control) == Keys.Control)     
            {
                return (IntPtr)1; // if 0 is returned then All the above keys will be enabled
            }
        }
        return CallNextHookEx(ptrHook, nCode, wp, lp);
    }

    bool HasAltModifier(int flags)
    {
        return (flags & 0x20) == 0x20;
    }

    /* Code to Disable WinKey, Alt+Tab, Ctrl+Esc Ends Here */

Then Inside the Form_Load();

   private void Form_Load(object sender, EventArgs e)
   {
      ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
      objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
      ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);  
   }
jdecuyper
  • 3,934
  • 9
  • 39
  • 51
ASr..
  • 458
  • 2
  • 7
  • 21
  • I get this error Error `System.Windows.Input.ModifierKeys' is a 'type' but is used like a 'variable` – Carlos Blanco Mar 13 '13 at 15:20
  • 1
    I ended up using this check `if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Tab && HasAltModifier(objKeyInfo.flags) || objKeyInfo.key == Keys.Escape && (Keyboard.Modifiers & ModifierKeys.Control) != 0)` – Carlos Blanco Mar 13 '13 at 15:28
  • I use this code, but i get error `Attempted to read or write protected memory. This is often an indication that other memory is corrupt ` – Niloo Jan 15 '14 at 19:58
  • 2
    Works well except Alt+Esc was still causing problems (tried CarlosBlanco's solution and it didn't fix it). Adding the condition `objKeyInfo.key == Keys.Escape && HasAltModifier(objKeyInfo.flags)` worked, however. – kad81 Feb 20 '14 at 04:37
  • Amazing solution, works great except for some reason after I exit alt + tab is still disabled. I tried using `UnhookWindowsHookEx` but it didn't seem to work – WolfyD Aug 05 '17 at 20:22
  • Thanks a lot Just that I needed to run the exe as Administrator. – AntiqTech Jan 03 '22 at 11:20
7

You can use the OnKeyDown event to capture the keys pressed and suppress the ones you don't want to allow.

Scott Hanselman's BabySmash application does disable most key strokes like alt-tab alt-esc, etc. Most of the source and development can be found on his blog. The source is on GitHub. In the source, you will see he InterceptKeys class that uses many win32 calls to get low level hooks to the keys pressed. He then handles these in HookCallback in the App.xaml.cs file. Hope this helps.

Similar Question

Another Similar

Community
  • 1
  • 1
Mike Ohlsen
  • 1,900
  • 12
  • 21