I remember if I was still using VB6 sereval years ago, if global hotkeys is required, a massive system-hooking pro Gram would be used.
That ' s terrible,wasn ' t it? :)
But in C #, this can be much easier. We can use System APIs: RegisterHotKey and Unregisterhotkey(this links to the official document of Micro Soft
What does next? Certianly is-to-encapsulate those function into a class-in order-to-use it rapidly. (Thanks for Sir.) Bomo for his template, which is the hispage)
The following class, the function Processkey is of great importance.
In the main form-class, a \ (void WndProc (reg Message m) \) are required,which used to processing the hotkey sends in.
Code: (After your register the hotkey,press ctrl+alt+s to hide/show you window.)
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten usingSystem.Runtime; One usingSystem.Runtime.InteropServices; A usingSystem.Diagnostics; - - namespaceWindowsFormsApplication1 the { - Public Partial classForm1:form - { -Hotkeys h =Newhotkeys (); + - PublicForm1 () + { A InitializeComponent (); at } - - Private voidBtnregist_click (Objectsender, EventArgs e) - { - if(H.regist ( This. Handle, (int) HotKeys.HotkeyModifiers.Shift + (int) HotKeys.HotkeyModifiers.Control, KEYS.S, CallBack)) -MessageBox.Show ("Success"); in ElseMessageBox.Show ("Failed"); - } to Private voidBtnunregist_click (Objectsender, EventArgs e) + { - if(H.unregist ( This. Handle, CallBack)) theMessageBox.Show ("Success"); * ElseMessageBox.Show ("Failed"); $ }Panax Notoginseng - protected Override voidWndProc (refMessage m) the { +H.processhotkey (m);//processing A Base. WndProc (refm); the } + - Public voidCallBack () $ { $ This. Visible =! This. Visible; - } - the Private voidForm1_Load (Objectsender, EventArgs e) - {Wuyi the } - Wu - About } $ - - Public classhotkeys - { A +[DllImport ("user32.dll")] the Static extern BOOLRegisterHotKey (IntPtr hWnd,intIdintmodifiers, Keys VK); -[DllImport ("user32.dll")] $ Static extern BOOLUnregisterhotkey (IntPtr hWnd,intID); the the the the Private intkeyID =Ten; - Privatedictionary<int, hotkeycallbackhanlder> keymap =Newdictionary<int, hotkeycallbackhanlder>(); in Public Delegate voidHotkeycallbackhanlder (); the Private Const intWm_hotkey =0x312; the About Public enumhotkeymodifiers the { theAlt =1, theControl =2, +Shift =4, -Win =8 the }Bayi the Public BOOLRegist (IntPtr hWnd,intmodifiers, Keys VK, Hotkeycallbackhanlder callBack)//regist Hotkey the { - Try - { the intid = keyid++; the if(! RegisterHotKey (hWnd, id, modifiers, VK))return false; theKeymap[id] =CallBack; the } - Catch{return false; }//Make the class stronger the return true; the } the 94 Public BOOLUnregist (IntPtr hWnd, Hotkeycallbackhanlder callBack)//UnRegister Hotkey the { the BOOLBX =false; the foreach(keyvaluepair<int, hotkeycallbackhanlder>var inchkeymap)98 { About if(var. Value = =callBack) - {101 Try102 {103Unregisterhotkey (HWnd,var. Key);104Keymap. Remove (var. Key);//I think this Important,which the original author not add. the }106 Catch { }107BX =true;108 }109 } the returnBX;111 the }113 the Public voidProcesshotkey (Message m)//Processing and React the { the if(m.msg = = Wm_hotkey)//The original author puts 0x312 here. Wm_hotkey can be clearer.117 {118 intID =M.wparam.toint32 ();119 Hotkeycallbackhanlder callback; - if(Keymap. TryGetValue (ID, outcallback))121 callback ();122 }123 }124 } the}
"C #" "SHARE" the Registering of global hotkeys