1. Form Hotkey
First, you set the main form KeyPreview to True, which you can set directly in the property.
or set in the form load: this. KeyPreview = true;
Then add the form KeyDown event, as follows:
Private void Frmmain_keydown (object sender, KeyEventArgs e)
{ if (e.alt && e.shift && e.control && e.keycode = = keys.s) { MessageBox.Show (" I pressed control +shift +alt +s");} }
2. Global Hotkey Settings
Define API function Register Hotkey "Uninstall Hotkey"
I define the Apphotkey class here, all the code is as follows:
Public classApphotkey {[DllImport ("Kernel32.dll")] Public Static extern UINTGetLastError (); //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. [DllImport ("user32.dll", SetLastError =true)] Public Static extern BOOLRegisterHotKey (IntPtr hWnd,//the handle of the window to define the hotkey intId//Define Hotkey ID (cannot be duplicated with other IDs)Keymodifiers Fsmodifiers,//identifies whether the hotkey will take effect when pressing ALT, Ctrl, Shift, Windows, and other keysKeys VK//defining the contents of a hotkey ); [DllImport ("user32.dll", SetLastError =true)] Public Static extern BOOLUnregisterhotkey (IntPtr hWnd,//handle of the window to cancel the hotkey intId//the ID of the hotkey to cancel ); //defines the name of the secondary key (converts the number to characters for easy memory, or removes the enumeration and uses the value directly)[Flags ()] Public enumkeymodifiers {None=0, Alt=1, Ctrl=2, Shift=4, WindowsKey=8 } /// <summary> ///Register Hotkey/// </summary> /// <param name= "hwnd" >window Handle</param> /// <param name= "hotkey_id" >Hotkey ID</param> /// <param name= "Keymodifiers" >Key Combinations</param> /// <param name= "key" >Hotkey</param> Public Static voidRegKey (INTPTR hwnd,inthotkey_id, Keymodifiers keymodifiers, Keys key) { Try { if(!RegisterHotKey (hwnd, HOTKEY_ID, Keymodifiers, key)) { if(Marshal.GetLastWin32Error () = =1409) {MessageBox.Show ("hot keys are occupied! "); } Else{MessageBox.Show ("Register Hotkey failed! "); } } } Catch(Exception) {}}/// <summary> ///Logout Hotkey/// </summary> /// <param name= "hwnd" >window Handle</param> /// <param name= "hotkey_id" >Hotkey ID</param> Public Static voidUnregkey (INTPTR hwnd,inthotkey_id) { //Logout hotkey setting with ID number hotkey_idUnregisterhotkey (hwnd, hotkey_id); } }
Rewrite the form's WndProc function, register the hotkey when the window is created, destroy the hotkey when the window is destroyed, and the code is as follows:
Private Const intWm_hotkey =0x312;//window Message-Hotkey Private Const intWm_create =0x1;//window messages-Create Private Const intWm_destroy =0x2;//window Message-Destroy Private Const intSpace =0x3572;//Hotkey ID protected Override voidWndProc (refMessage m) { Base. WndProc (refm); Switch(m.msg) { CaseWm_hotkey://window message-Hotkey ID Switch(M.wparam.toint32 ()) { CaseSpace://Hotkey IDMessageBox.Show ("I pressed the control +shift +alt +s"); Break; default: Break; } Break; CaseWm_create://window messages-CreateApphotkey.regkey (Handle, Space, AppHotKey.KeyModifiers.Ctrl | AppHotKey.KeyModifiers.Shift |AppHotKey.KeyModifiers.Alt, KEYS.S); Break; CaseWm_destroy://window Message-DestroyApphotkey.unregkey (Handle, Space);//destroying Hotkeys Break; default: Break; } }
C # Global Hotkey settings and form hotkey settings