using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace ULR { class Hotkey : System.Windows.Forms.Form { [DllImport("user32.dll")] private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk); [DllImport("user32.dll")] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); public const int MOD_ALT = 0x1; public const int MOD_CONTROL = 0x2; public const int MOD_SHIFT = 0x4; public const int MOD_WIN = 0x8; private const int WM_HOTKEY = 0x312; public delegate void OnHotkeyHandler(); public event OnHotkeyHandler OnHotkey; private static int _identifier = 0; private int identifier = -1; public Hotkey(int modifiers, int vk) { this.Visible = false; identifier = ++_identifier; RegisterHotKey(this.Handle, identifier, modifiers, vk); } protected override void Dispose(bool disposing) { if (disposing) { UnregisterHotKey(this.Handle, identifier); } base.Dispose(disposing); } protected override void WndProc(ref System.Windows.Forms.Message m) { base.WndProc(ref m); if (m.Msg == WM_HOTKEY) { if (OnHotkey != null) { OnHotkey(); } } } } }