daily pastebin goal
64%
SHARE
TWEET

Keyboard Hook Block ComboKeys C#

a guest Mar 13th, 2018 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace BlockShortcuts
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private delegate int LowLevelKeyboardProcDelegate(int nCode, int
  11.            wParam, ref KBDLLHOOKSTRUCT lParam);
  12.  
  13.         [DllImport("user32.dll", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi)]
  14.         private static extern int SetWindowsHookEx(
  15.            int idHook,
  16.            LowLevelKeyboardProcDelegate lpfn,
  17.            int hMod,
  18.            int dwThreadId);
  19.  
  20.         [DllImport("user32.dll")]
  21.         private static extern int UnhookWindowsHookEx(int hHook);
  22.  
  23.         [DllImport("user32.dll", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi)]
  24.         private static extern int CallNextHookEx(
  25.             int hHook, int nCode,
  26.             int wParam, ref KBDLLHOOKSTRUCT lParam);
  27.  
  28.         const int WH_KEYBOARD_LL = 13;
  29.         private int intLLKey;
  30.         private KBDLLHOOKSTRUCT lParam;
  31.  
  32.         private struct KBDLLHOOKSTRUCT
  33.         {
  34.             public int vkCode;
  35.             int scanCode;
  36.             public int flags;
  37.             int time;
  38.             int dwExtraInfo;
  39.         }
  40.  
  41.         private int LowLevelKeyboardProc(
  42.             int nCode, int wParam,
  43.             ref KBDLLHOOKSTRUCT lParam)
  44.         {
  45.             bool blnEat = false;
  46.             switch (wParam)
  47.             {
  48.                 case 256:
  49.                 case 257:
  50.                 case 260:
  51.                 case 261:
  52.                     //Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
  53.                     if (((lParam.vkCode == 9) && (lParam.flags == 32)) ||
  54.                     ((lParam.vkCode == 27) && (lParam.flags == 32)) || ((lParam.vkCode ==
  55.                     27) && (lParam.flags == 0)) || ((lParam.vkCode == 91) && (lParam.flags
  56.                     == 1)) || ((lParam.vkCode == 92) && (lParam.flags == 1)) || ((true) &&
  57.                     (lParam.flags == 32)))
  58.                     {
  59.                         blnEat = true;
  60.                     }
  61.                     break;
  62.             }
  63.  
  64.             if (blnEat)
  65.                 return 1;
  66.             else return CallNextHookEx(0, nCode, wParam, ref lParam);
  67.  
  68.         }
  69.  
  70.         private void KeyboardHook(object sender, EventArgs e)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top