Sunday, November 17, 2013

C# System wide shortcut keys | Global Hot-key in C#

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4. namespace WindowsFormsApplication1
  5. {
  6.         public partial class Form1 : Form
  7.         {
  8.                 private static int _hookHandle = 0;
  9.                 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  10.                 public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, intthreadId);
  11.                 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  12.                 public static extern bool UnhookWindowsHookEx(int idHook);
  13.                 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  14.                 public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
  15.                 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  16.                 public static extern short GetKeyState(int nVirtKey);
  17.                 public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
  18.                 public const int WH_KEYBOARD_LL = 13; 
  19.                 public const int VK_LCONTROL = 0xA2;
  20.                 public const int VK_RCONTROL = 0xA3;
  21.                 public Form1()
  22.                 {
  23.                         InitializeComponent();
  24.                         SetHook();
  25.                 }

  26.                 private void SetHook()
  27.                 {
  28.                         // Set system-wide hook.
  29.                         _hookHandle = SetWindowsHookEx(
  30.                                 WH_KEYBOARD_LL,
  31.                                 KbHookProc,
  32.                                 (IntPtr)0,
  33.                                 0);
  34.                 }
  35.                 private static int KbHookProc(int nCode, IntPtr wParam, IntPtr Param)
  36.                 {
  37.                         if (nCode >= 0)
  38.                         {                                var hookStruct = (KbLLHookStruct)Marshal.PtrToStructure(lParam,typeof(KbLLHookStruct));
  39.                                 // Quick and dirty check. You may need to check if this is correct. See GetKeyState for more info.
  40.                                 bool ctrlDown = GetKeyState(VK_LCONTROL) != 0 || GetKeyState(VK_RCONTROL) != 0;
  41.                                 if (ctrlDown && hookStruct.vkCode == 0x56) // Ctrl+V
  42.                                 {
  43.                                         Clipboard.SetText("Hi"); // Replace this with yours something here...
  44.                                 }
  45.                         }
  46.                         // Pass to other keyboard handlers. Makes the Ctrl+V pass through.
  47.                         return CallNextHookEx(_hookHandle, nCode, wParam, lParam);
  48.                 }
  49.                 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  50.                 {
  51.                         UnhookWindowsHookEx(_hookHandle);
  52.                 }
  53.                 //Declare the wrapper managed MouseHookStruct class.
  54.                 [StructLayout(LayoutKind.Sequential)]
  55.                 public class KbLLHookStruct
  56.                 {
  57.                         public int vkCode;
  58.                         public int scanCode;
  59.                         public int flags;
  60.                         public int time;
  61.                         public int dwExtraInfo;
  62.                 }
  63.         }
  64. }

No comments:

Post a Comment