Advertisement
Guest User

C# Low-level Keyboard Hook

a guest
Jul 26th, 2011
1,930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4.  
  5. namespace WindowsFormsApplication1
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         private static int _hookHandle = 0;
  10.  
  11.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  12.         public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
  13.  
  14.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  15.         public static extern bool UnhookWindowsHookEx(int idHook);
  16.  
  17.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  18.         public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
  19.  
  20.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  21.         public static extern short GetKeyState(int nVirtKey);
  22.  
  23.         public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
  24.  
  25.         public const int WH_KEYBOARD_LL = 13;
  26.  
  27.         public const int VK_LCONTROL = 0xA2;
  28.         public const int VK_RCONTROL = 0xA3;
  29.  
  30.         public Form1()
  31.         {
  32.             InitializeComponent();
  33.             SetHook();
  34.         }
  35.  
  36.         private void SetHook()
  37.         {
  38.             // Set system-wide hook.
  39.             _hookHandle = SetWindowsHookEx(
  40.                 WH_KEYBOARD_LL,
  41.                 KbHookProc,
  42.                 (IntPtr)0,
  43.                 0);
  44.         }
  45.  
  46.         private static int KbHookProc(int nCode, IntPtr wParam, IntPtr lParam)
  47.         {
  48.             if (nCode >= 0)
  49.             {
  50.                 var hookStruct = (KbLLHookStruct)Marshal.PtrToStructure(lParam, typeof(KbLLHookStruct));
  51.  
  52.                 // Quick and dirty check. You may need to check if this is correct. See GetKeyState for more info.
  53.                 bool ctrlDown = GetKeyState(VK_LCONTROL) != 0 || GetKeyState(VK_RCONTROL) != 0;
  54.  
  55.                 if (ctrlDown && hookStruct.vkCode == 0x56) // Ctrl+V
  56.                 {
  57.                     Clipboard.SetText("Hi"); // Replace this with yours something here...
  58.                 }
  59.             }
  60.  
  61.             // Pass to other keyboard handlers. Makes the Ctrl+V pass through.
  62.             return CallNextHookEx(_hookHandle, nCode, wParam, lParam);
  63.         }
  64.  
  65.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  66.         {
  67.             UnhookWindowsHookEx(_hookHandle);
  68.         }
  69.  
  70.         //Declare the wrapper managed MouseHookStruct class.
  71.         [StructLayout(LayoutKind.Sequential)]
  72.         public class KbLLHookStruct
  73.         {
  74.             public int vkCode;
  75.             public int scanCode;
  76.             public int flags;
  77.             public int time;
  78.             public int dwExtraInfo;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement