Advertisement
electricmaster

Key hook

May 17th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Input;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7.  
  8. // Adapted from http://www.dylansweb.com/2014/10/low-level-global-keyboard-hook-sink-in-c-net/
  9.  
  10. namespace Compose_Key_for_Windows
  11. {
  12.     class KeyHook
  13.     {
  14.         private const int WH_KEYBOARD_LL = 13;
  15.         private const int WM_KEYDOWN = 0x0100;
  16.         private const int WM_SYSKEYDOWN = 0x0104;
  17.  
  18.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  19.         private static extern IntPtr SetWindowsHookEx(int idHook, LLKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  20.  
  21.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  22.         [return: MarshalAs(UnmanagedType.Bool)]
  23.         private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  24.  
  25.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  26.         private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  27.  
  28.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  29.         private static extern IntPtr GetModuleHandle(string lpModuleName);
  30.  
  31.         public delegate IntPtr LLKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
  32.  
  33.         public event EventHandler<KeyPressedArgs> OnKeyPressed;
  34.  
  35.         private LLKeyboardProc _proc;
  36.         private IntPtr _hookID = IntPtr.Zero;
  37.  
  38.         public void LLKeyboardListener()
  39.         {
  40.             _proc = HookCallback;
  41.         }
  42.  
  43.         public void HookKeyboard()
  44.         {
  45.             _hookID = SetHook(_proc);
  46.         }
  47.  
  48.         public void UnhookKeyboard()
  49.         {
  50.             UnhookWindowsHookEx(_hookID);
  51.         }
  52.  
  53.         private IntPtr SetHook(LLKeyboardProc proc)
  54.         {
  55.             using (Process curProcess = Process.GetCurrentProcess())
  56.             using (ProcessModule curModule = curProcess.MainModule)
  57.             {
  58.                 return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
  59.             }
  60.         }
  61.  
  62.         private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  63.         {
  64.             if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
  65.             {
  66.                 int vkCode = Marshal.ReadInt32(lParam);
  67.  
  68.                 if (OnKeyPressed != null)
  69.                 {
  70.                     KeysConverter kc = new KeysConverter();
  71.                     OnKeyPressed(this, new KeyPressedArgs((Keys)Enum.Parse(typeof(Keys), kc.ConvertToString(vkCode))));
  72.                 }
  73.             }
  74.             return CallNextHookEx(_hookID, nCode, wParam, lParam);
  75.         }
  76.     }
  77.  
  78.     public class KeyPressedArgs : EventArgs
  79.     {
  80.         public Keys KeyPressed { get; private set; }
  81.  
  82.         public KeyPressedArgs(Keys key)
  83.         {
  84.             KeyPressed = key;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement