Guest User

Keyboard hook freeze

a guest
Oct 16th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1.     public class Program
  2.     {
  3.         /*
  4.          * Hook Variables
  5.          */
  6.         private const int WH_KEYBOARD_LL = 13;
  7.         private const int WM_KEYDOWN = 0x0100;
  8.         private const int VK_F1 = 0x70;
  9.         private static LowLevelKeyboardProc _proc = HookCallback;
  10.         private static IntPtr _hookID = IntPtr.Zero;
  11.  
  12.         /*
  13.          * Hook DllImports
  14.          */
  15.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  16.         private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  17.  
  18.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  19.         [return: MarshalAs(UnmanagedType.Bool)]
  20.         private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  21.  
  22.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  23.         private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  24.  
  25.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  26.         private static extern IntPtr GetModuleHandle(string lpModuleName);
  27.  
  28.         /*
  29.          * Variables
  30.          */
  31.         public delegate void TheKeyEventHandler();
  32.         public static event TheKeyEventHandler OnKey;
  33.         private static Program instance = new Program();
  34.         public static Program Instance
  35.         {
  36.             get
  37.             {
  38.                 return instance;
  39.             }
  40.         }
  41.  
  42.         /*
  43.          * Main
  44.          */
  45.         [STAThread]
  46.         static void Main()
  47.         {
  48.            
  49.         }
  50.  
  51.         /*
  52.          * Constructor
  53.          */
  54.         private Program()
  55.         {
  56.             Application.EnableVisualStyles();
  57.             Application.SetCompatibleTextRenderingDefault(false);
  58.             _hookID = SetHook(_proc);
  59.             Form = new Form1();
  60.             OnKey += Program_OnKeyPress;
  61.             Application.Run(Form);
  62.             UnhookWindowsHookEx(_hookID);
  63.         }
  64.  
  65.         void Program_OnKeyPress()
  66.         {
  67.             // Interact with the form.
  68.         }
  69.  
  70.         /*
  71.          * Hook Methods
  72.          */
  73.         private static IntPtr SetHook(LowLevelKeyboardProc proc)
  74.         {
  75.             using (Process curProcess = Process.GetCurrentProcess())
  76.             using (ProcessModule curModule = curProcess.MainModule)
  77.             {
  78.                 return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
  79.             }
  80.         }
  81.  
  82.         private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
  83.  
  84.         private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  85.         {
  86.             if (nCode >= 0)
  87.             {
  88.                 Keys number = (Keys)Marshal.ReadInt32(lParam);
  89.                 OnKey();
  90.             }
  91.             return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
  92.         }
  93.     }
Advertisement
Add Comment
Please, Sign In to add comment