Advertisement
Uziel

GlobalKeyboardHookLibrary

Oct 28th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6.  
  7. namespace Utilities {
  8.     /// <summary>
  9.     /// A class that manages a global low level keyboard hook
  10.     /// </summary>
  11.     class GlobalKeyboardHook {
  12.         #region Constant, Structure and Delegate Definitions
  13.         /// <summary>
  14.         /// defines the callback type for the hook
  15.         /// </summary>
  16.         public delegate int KeyboardHookProc(int code, int wParam, ref KeyboardHookStruct lParam);
  17.  
  18.         public struct KeyboardHookStruct {
  19.             public int VkCode;
  20.             public int ScanCode;
  21.             public int Flags;
  22.             public int Time;
  23.             public int DwExtraInfo;
  24.         }
  25.  
  26.         const int WhKeyboardLl = 13;
  27.         const int WmKeydown = 0x100;
  28.         const int WmKeyup = 0x101;
  29.         const int WmSyskeydown = 0x104;
  30.         const int WmSyskeyup = 0x105;
  31.         #endregion
  32.  
  33.         #region Instance Variables
  34.         /// <summary>
  35.         /// The collections of keys to watch for
  36.         /// </summary>
  37.         public List<Keys> HookedKeys = new List<Keys>();
  38.         /// <summary>
  39.         /// Handle to the hook, need this to unhook and call the next hook
  40.         /// </summary>
  41.         IntPtr _hhook = IntPtr.Zero;
  42.         #endregion
  43.  
  44.         #region Events
  45.         /// <summary>
  46.         /// Occurs when one of the hooked keys is pressed
  47.         /// </summary>
  48.         public event KeyEventHandler KeyDown;
  49.         /// <summary>
  50.         /// Occurs when one of the hooked keys is released
  51.         /// </summary>
  52.         public event KeyEventHandler KeyUp;
  53.         #endregion
  54.  
  55.         #region Constructors and Destructors
  56.         /// <summary>
  57.         /// Initializes a new instance of the <see cref="GlobalKeyboardHook"/> class and installs the keyboard hook.
  58.         /// </summary>
  59.         public GlobalKeyboardHook() {
  60.             Hook();
  61.         }
  62.  
  63.         /// <summary>
  64.         /// Releases unmanaged resources and performs other cleanup operations before the
  65.         /// <see cref="GlobalKeyboardHook"/> is reclaimed by garbage collection and uninstalls the keyboard hook.
  66.         /// </summary>
  67.         ~GlobalKeyboardHook() {
  68.             Unhook();
  69.         }
  70.         #endregion
  71.  
  72.         #region Public Methods
  73.         /// <summary>
  74.         /// Installs the global hook
  75.         /// </summary>
  76.         public void Hook() {
  77.             IntPtr hInstance = LoadLibrary("User32");
  78.             _hhook = SetWindowsHookEx(WhKeyboardLl, HookProc, hInstance, 0);
  79.         }
  80.  
  81.         /// <summary>
  82.         /// Uninstalls the global hook
  83.         /// </summary>
  84.         public void Unhook() {
  85.             UnhookWindowsHookEx(_hhook);
  86.         }
  87.  
  88.         /// <summary>
  89.         /// The callback for the keyboard hook
  90.         /// </summary>
  91.         /// <param name="code">The hook code, if it isn't >= 0, the function shouldn't do anything</param>
  92.         /// <param name="wParam">The event type</param>
  93.         /// <param name="lParam">The keyhook event information</param>
  94.         /// <returns></returns>
  95.         public int HookProc(int code, int wParam, ref KeyboardHookStruct lParam) {
  96.             if (code < 0) return CallNextHookEx(_hhook, code, wParam, ref lParam);
  97.             Keys key = (Keys)lParam.VkCode;
  98.             if (!HookedKeys.Contains(key)) return CallNextHookEx(_hhook, code, wParam, ref lParam);
  99.             KeyEventArgs keyEventArgs = new KeyEventArgs(key);
  100.             if ((wParam == WmKeydown || wParam == WmSyskeydown) && (KeyDown != null)) {
  101.                 KeyDown(this, keyEventArgs) ;
  102.             } else if ((wParam == WmKeyup || wParam == WmSyskeyup) && (KeyUp != null)) {
  103.                 KeyUp(this, keyEventArgs);
  104.             }
  105.             return keyEventArgs.Handled ? 1 : CallNextHookEx(_hhook, code, wParam, ref lParam);
  106.         }
  107.         #endregion
  108.  
  109.         #region DLL imports
  110.         /// <summary>
  111.         /// Sets the windows hook, do the desired event, one of hInstance or threadId must be non-null
  112.         /// </summary>
  113.         /// <param name="idHook">The id of the event you want to hook</param>
  114.         /// <param name="callback">The callback.</param>
  115.         /// <param name="hInstance">The handle you want to attach the event to, can be null</param>
  116.         /// <param name="threadId">The thread you want to attach the event to, can be null</param>
  117.         /// <returns>a handle to the desired hook</returns>
  118.         [DllImport("user32.dll")]
  119.         static extern IntPtr SetWindowsHookEx(int idHook, KeyboardHookProc callback, IntPtr hInstance, uint threadId);
  120.  
  121.         /// <summary>
  122.         /// Unhooks the windows hook.
  123.         /// </summary>
  124.         /// <param name="hInstance">The hook handle that was returned from SetWindowsHookEx</param>
  125.         /// <returns>True if successful, false otherwise</returns>
  126.         [DllImport("user32.dll")]
  127.         static extern bool UnhookWindowsHookEx(IntPtr hInstance);
  128.  
  129.         /// <summary>
  130.         /// Calls the next hook.
  131.         /// </summary>
  132.         /// <param name="idHook">The hook id</param>
  133.         /// <param name="nCode">The hook code</param>
  134.         /// <param name="wParam">The wparam.</param>
  135.         /// <param name="lParam">The lparam.</param>
  136.         /// <returns></returns>
  137.         [DllImport("user32.dll")]
  138.         static extern int CallNextHookEx(IntPtr idHook, int nCode, int wParam, ref KeyboardHookStruct lParam);
  139.  
  140.         /// <summary>
  141.         /// Loads the library.
  142.         /// </summary>
  143.         /// <param name="lpFileName">Name of the library</param>
  144.         /// <returns>A handle to the library</returns>
  145.         [DllImport("kernel32.dll")]
  146.         static extern IntPtr LoadLibrary(string lpFileName);
  147.         #endregion
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement