usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.InteropServices;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Forms;usingSystem.Windows.Interop;namespacewpfhotkey{ Public classHotkeyhelper {/// <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" >the handle of the window to define the hotkey</param> /// <param name= "id" >Define Hotkey ID (cannot be duplicated with other IDs)</param> /// <param name= "Fsmodifiers" >identifies whether the hotkey will take effect when pressing ALT, Ctrl, Shift, Windows, and other keys</param> /// <param name= "VK" >defines the contents of the hotkey, which can be converted using the Keys enumeration in WinForm ,///The key enumeration in WPF is incorrect, you should use the System.Windows.Forms.Keys enumeration, or customize the correct enumeration or int constants</param> /// <returns></returns>[DllImport ("user32.dll", SetLastError =true)] Static extern BOOLRegisterHotKey (IntPtr hWnd,intID, keymodifiers fsmodifiers,intVK); /// <summary> ///Cancel Registration Hotkey/// </summary> /// <param name= "HWnd" >handle of the window to cancel the hotkey</param> /// <param name= "id" >the ID of the hotkey to cancel</param> /// <returns></returns>[DllImport ("user32.dll", SetLastError =true)] Static extern BOOLUnregisterhotkey (IntPtr hWnd,intID); /// <summary> ///adds a string to the global atomic table and returns a unique identifier for the string, and succeeds returns the newly created atomic ID, which fails to return 0/// </summary> /// <param name= "lpstring" ></param> /// <returns></returns>[DllImport ("kernel32", SetLastError =true)] Static extern ShortGlobaladdatom (stringlpstring); [DllImport ("kernel32", SetLastError =true)] Static extern ShortGlobaldeleteatom ( Shortnatom); /// <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>[Flags ()] Public enumkeymodifiers {None=0, Alt=1, Ctrl=2, Shift=4, WindowsKey=8 } /// <summary> ///the corresponding message ID of the Hotkey/// </summary> Public Const intWm_hotkey =0x312; Staticdictionary<int, action> keymap =Newdictionary<int, action> ();//each key for a handler function /// <summary> ///Register shortcut Keys/// </summary> /// <param name= "window" >main form</param> /// <param name= "Action" >method of Callback</param> /// <returns>identification of the hotkey</returns> Public Static voidregist (Window window, action action) {HwndSource HwndSource; Windowinterophelper wih=Newwindowinterophelper (window); HWndSource=Hwndsource.fromhwnd (wih. Handle); //Adding HandlersHwndsource.addhook (MAINWINDOWPROC); intKeycodes =-1; if(! RegisterHotKey (wih. Handle, Keycodes, HotKeyHelper.KeyModifiers.Ctrl, (int) (KEYS.S)) {Throw NewException ("Registration Failed"); } Else { if(Keymap. Where (p = = P.key = = keycodes). Count () >0) Throw NewException ("registration failed, should be registered for this key combination!! "); Else{keymap. ADD (keycodes, action); } } } /// <summary> ///Uninstall shortcut keys/// </summary> /// <param name= "window" >main form</param> /// <param name= "CallBack" >method of callback at registration</param> Public Static voidunregist (Window window, Action callBack) {foreach(keyvaluepair<int, action>var inchkeymap) {Windowinterophelper wih=Newwindowinterophelper (window); if(var. Value = =callBack) {Unregisterhotkey (wih). Handle,var. Key); Keymap. Remove (var. Key); return; } } } Private StaticINTPTR Mainwindowproc (INTPTR hwnd,intMSG, IntPtr WParam, IntPtr LParam,ref BOOLhandled) { Switch(msg) { CaseHotkeyhelper.wm_hotkey: {intSid =Wparam.toint32 (); if(Keymap. Where (p = = P.key = = SID). Count () >0) {Keymap. Where (P= = P.key = =SID). FirstOrDefault (). Value.invoke (); } Handled=true; Break; } } returnIntPtr.Zero; } }}
A class of WPF program Hotkeys