question

AlvaroorregoGalan-9513 avatar image
0 Votes"
AlvaroorregoGalan-9513 asked Castorix31 answered

Anyone know how to disable alt+tab or only alt when application is open?

Im making an application and I need the code to do this. Thanks.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered

You can use a WH_KEYBOARD_LL Hook

A test, on Windows 10 1909 (click on the Button to start the Hook which disables Alt + Tab) =>

 public partial class Form1 : Form
 {
     public const int WH_MIN = (-1);
     public const int WH_MSGFILTER = (-1);
     public const int WH_JOURNALRECORD = 0;
     public const int WH_JOURNALPLAYBACK = 1;
     public const int WH_KEYBOARD = 2;
     public const int WH_GETMESSAGE = 3;
     public const int WH_CALLWNDPROC = 4;
     public const int WH_CBT = 5;
     public const int WH_SYSMSGFILTER = 6;
     public const int WH_MOUSE = 7;
     public const int WH_HARDWARE = 8;
     public const int WH_DEBUG = 9;
     public const int WH_SHELL = 10;
     public const int WH_FOREGROUNDIDLE = 11;
     public const int WH_CALLWNDPROCRET = 12;
     public const int WH_KEYBOARD_LL = 13;
     public const int WH_MOUSE_LL = 14;
     public const int WH_MAX = 14;
     public const int WH_MINHOOK = WH_MIN;
     public const int WH_MAXHOOK = WH_MAX;

     [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
     public struct MSLLHOOKSTRUCT
     {
         public System.Drawing.Point pt;
         public int mouseData;
         public int flags;
         public int time;
         public uint dwExtraInfo;
     }

     [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
     public struct KBDLLHOOKSTRUCT
     {
         public int vkCode;
         public int scanCode;
         public int flags;
         public int time;
         public uint dwExtraInfo;
     }

     public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

     [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
     public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

     [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
     public static extern bool UnhookWindowsHookEx(int idHook);

     [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
     public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);

     public const int KF_EXTENDED = 0x0100;
     public const int KF_DLGMODE = 0x0800;
     public const int KF_MENUMODE = 0x1000;
     public const int KF_ALTDOWN = 0x2000;
     public const int KF_REPEAT = 0x4000;
     public const int KF_UP = 0x8000;

     public const int LLKHF_EXTENDED = (KF_EXTENDED >> 8); /* 0x00000001 */
     public const int LLKHF_INJECTED = 0x00000010;
     public const int LLKHF_ALTDOWN = (KF_ALTDOWN >> 8); /* 0x00000020 */
     public const int LLKHF_UP = (KF_UP >> 8);      /* 0x00000080 */
     public const int LLKHF_LOWER_IL_INJECTED = 0x00000002;

     public const int LLMHF_INJECTED = 0x00000001;
     public const int LLMHF_LOWER_IL_INJECTED = 0x00000002;

     static int hHook = 0;
     HookProc KeyboardLLProcedure;

     public const int WM_KEYDOWN = 0x0100;
     public const int WM_SYSKEYDOWN = 0x0104;
     public const int VK_SHIFT = 0x10;
     public const int VK_MENU = 0x12;

     [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
     private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);

     public static int KeyboardLLProc(int nCode, IntPtr wParam, IntPtr lParam)
     {
         bool bShiftKeyDown = false;
         bool bAltKeyDown = false;

         if (nCode >= 0)
         {
             bShiftKeyDown = GetAsyncKeyState((Keys)VK_SHIFT) < 0;
             bAltKeyDown = GetAsyncKeyState((Keys)VK_MENU) < 0;
             KBDLLHOOKSTRUCT pKBDLLHOOKSTRUCT = new KBDLLHOOKSTRUCT();
             pKBDLLHOOKSTRUCT = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, pKBDLLHOOKSTRUCT.GetType());

             //if (pKBDLLHOOKSTRUCT.vkCode == (short)Keys.LWin)
             if (pKBDLLHOOKSTRUCT.vkCode == (short)Keys.Tab && bAltKeyDown)
             {
                 Console.Beep(8000, 10);
                 return 1;
             }
         }
         return nCode < 0 ? CallNextHookEx(hHook, nCode, wParam, lParam) : 0;
     }

     private System.Windows.Forms.Button button1;
     public Form1()
     {
         //InitializeComponent();
         this.button1 = new System.Windows.Forms.Button();
         this.button1.Location = new System.Drawing.Point(104, 114);
         this.button1.Name = "button1";
         this.button1.Size = new System.Drawing.Size(75, 23);
         this.button1.TabIndex = 0;
         this.button1.Text = "button1";
         this.button1.UseVisualStyleBackColor = true;
         this.button1.Click += new System.EventHandler(this.button1_Click);
         this.Controls.Add(this.button1);
         this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
         CenterToScreen();
     }

     private void button1_Click(object sender, EventArgs e)
     {
         if (hHook == 0)
         {
             KeyboardLLProcedure = new HookProc(KeyboardLLProc);
             hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardLLProcedure, (IntPtr)0, 0);
         }
     }

     private void Form1_FormClosing(object sender, FormClosingEventArgs e)
     {
         if (hHook != 0)
             UnhookWindowsHookEx(hHook);
     }      
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.