Because. NET does not provide a dedicated class library processing hotkey, so you need to directly call the Windows API to resolve.
Hotkey encapsulates code for. NET calls to the Windows API, primarily RegisterHotKey and Unregisterhotkey
Class HotKey {//<summary>///If the function executes successfully, the return value is not 0. If the function fails to execute, the return value is 0. To get the extended error message, call GetLastError. net method: Marshal.GetLastWin32Error ()///</summary>//<param name= "HWnd" > Handle of the window to define the hotkey </param& Gt <param name= "id" > Define Hotkey ID (cannot be duplicated with other IDs) </param>//<param name= "fsmodifiers" > Identify if Hotkey is pressed ALT, Ctrl, S Hift, Windows and other keys will only take effect </param>//<param name= "VK" > Define the contents of the hotkey, WinForm can use the Keys enumeration conversion,///WPF key enumeration is not Correctly, you should use the System.Windows.Forms.Keys enumeration, or customize the correct enumeration or int constants </param>///<returns></returns> [Dl Limport ("user32.dll", SetLastError = true)] public static extern bool RegisterHotKey (IntPtr hWnd, in T ID, keymodifiers fsmodifiers, int VK); <summary>//Cancel Registration Hotkey///</summary>//<param name= "HWnd" > The handle of the window to cancel the hotkey </p aram>//<param name= "id" > to cancel the hotkey Id</param>//<returns></returns> [DllImport ("user32.dll", SetLastError = True)] Publ IC static extern bool Unregisterhotkey (INTPTR hWnd, int id); <summary>/////Add a string to the global atomic table and return a unique identifier for the string, and a successful return value is the newly created atomic ID, which fails to return 0///</summary>// /<param name= "lpstring" ></param>///<returns></returns> [DllImport ("kernel32", S Etlasterror = true)] public static extern short Globaladdatom (string lpstring); [DllImport ("kernel32", SetLastError = true)] public static extern short Globaldeleteatom (short natom); <summary>////defines the name of the secondary key (converts the number to characters for easy memory, or removes the enumeration and uses the value directly)///</summary> [ComVisible (t Rue)] [Flags] [TypeConverter (typeof (KeysConverter))] public enum Keymodifiers {Non E = 0, Alt = 1, Ctrl = 2, Shift = 4, WindowsKey = 8}}
Calling code in Windows form
private void Form1_activated (object sender, EventArgs e) {Hotkey.registerhotkey (Handle, 102, Ho TKey.KeyModifiers.Alt | HotKey.KeyModifiers.Ctrl, KEYS.S); Hotkey.registerhotkey (Handle, HotKey.KeyModifiers.Shift, KEYS.S); } private void Form1_leave (object sender, EventArgs e) {Hotkey.unregisterhotkey (Handle, 102); Hotkey.unregisterhotkey (Handle, 100); }////////To monitor Windows messages///overloaded WndProc method to implement Hotkey response/////protected override Voi D WndProc (ref Message m) {const int wm_hotkey = 0x0312;//if M. The value of MSG is 0x0312 so that the user presses the hotkey//press the shortcut key switch (m.msg) {case Wm_hotkey: Switch (M.wparam.toint32 ()) {case 100://pressed is Shift+s The shortcut Key response code MessageBox.Show ("pressed shift+s") is filled in here. Break Case 101://press CTRL+B///here to fill in the shortcut key response code break; Case 102://Press Ctrl+alt+s Captureimagetool capture = new Captureimagetool (); if (capture. ShowDialog () = = DialogResult.OK) {image image = Capture. Image; Picturebox1.width = image. Width; Picturebox1.height = image. Height; pictureBox1.Image = Image; } break; } break; } base. WndProc (ref m); }
The window handle in WPF needs to be windowinterophelper, and the addition and WinForm of the handler function require HwndSource to add the handler.
void Mainwindow_unloaded (object sender, RoutedEventArgs e) {unregisterhotkey (); } void Mainwindow_loaded (object sender, RoutedEventArgs e) {RegisterHotKey (); private void Unregisterhotkey () {INTPTR handle = new Windowinterophelper (this). Handle; int id = 1000; BOOL R = Hotkey.unregisterhotkey (handle, ID); Hotkey.unregisterhotkey (handle, 102); Hotkey.unregisterhotkey (handle, 100); private void RegisterHotKey () {INTPTR handle = new Windowinterophelper (this). Handle; int id = 1000; BOOL R = Hotkey.registerhotkey (handle, ID, HotKey.KeyModifiers.Ctrl, (int) System.Windows.Forms.Keys.F12); Hotkey.registerhotkey (handle, 102, HOTKEY.KEYMODIFIERS.ALT | HotKey.KeyModifiers.Ctrl, (int) KEYS.S); Hotkey.registerhotkey (handle, N, HotKey.KeyModifiers.Shift, (int.) KEYS.S); Get the messageSources System.Windows.Interop.HwndSource Source = System.Windows.Interop.HwndSource.FromHwnd (handle); Source. Addhook (Hotkeyhook); } Static Private IntPtr Hotkeyhook (IntPtr hwnd, int msg, IntPtr wParam, IntPtr LParam, ref BOOL handled)//Hot Key process {Const int wm_hotkey = 0x0312;//if M. The value of MSG is 0x0312 so that the user presses the hotkey if (msg = = Wm_hotkey) {switch (Wparam.toint32 ()) {Case 100://press the Shift+s//here to fill in the shortcut key response code MessageBox . Show ("pressed Shift+s"); Break Case 101://press CTRL+B///here to fill in the shortcut key response code break; Case 102://press ctrl+alt+s break; }} return IntPtr.Zero; }
Registering hotkeys in WinForm and WPF