Public classNativeWIN32 { PublicNativeWIN32 () {}/*-------using WIN32 Windows API in a C # application-------*/[DllImport ("user32.dll", CharSet =CharSet.Auto)]Static Public externIntPtr GetForegroundWindow ();//[StructLayout (layoutkind.sequential, CharSet=CharSet.Auto)] Public structStringBuffer {[MarshalAs (UnmanagedType.ByValTStr, SizeConst= the)] Public stringSztext; } [DllImport ("user32.dll", CharSet =CharSet.Auto)] Public Static extern intGetWindowText (IntPtr hWnd, outStringBuffer ClassName,intnMaxCount); [DllImport ("user32.dll", CharSet =CharSet.Auto)] Public Static extern intSendMessage (IntPtr hWnd,intMsgintWParam,intLParam); [DllImport ("user32.dll", CharSet =CharSet.Auto)] Public Static externIntPtr SendMessage (IntPtr hWnd,intMsgintWParam, IntPtr lParam); Public Const intWm_syscommand =0x0112; Public Const intSc_close =0xf060; Public Delegate BOOLEnumthreadproc (IntPtr hwnd, INTPTR LParam); [DllImport ("user32.dll", CharSet =CharSet.Auto)] Public Static extern BOOLEnumThreadWindows (intthreadId, Enumthreadproc pfnenum, IntPtr lParam); [DllImport ("user32.dll", CharSet =CharSet.Auto)] Public Static externIntPtr FindWindowEx (IntPtr parent, IntPtr Next,stringSclassname, IntPtr swindowtitle); /*-------using Hotkeys in a C # application-------in form load:bool success = RegisterHotKey (Handle , Keymodifiers.control | Keymodifiers.shift, KEYS.J); In form Closing:unregisterhotkey (Handle, 100); protected override void WndProc (ref Message m) {const int wm_hotkey = 0x0312; Switch (m.msg) {case WM_HOTKEY:MessageBox.Show ("HOTKEY pressed"); Break } base. WndProc (ref m); }-------using Hotkeys in a C # application-------*/[DllImport ("user32.dll", SetLastError =true)] Public Static extern BOOLRegisterHotKey (IntPtr hWnd,//Handle to Window intId//Hot Key IdentifierKeymodifiers Fsmodifiers,//key-modifier OptionsKeys VK//Virtual-key Code ); [DllImport ("user32.dll", SetLastError =true)] Public Static extern BOOLUnregisterhotkey (IntPtr hWnd,//Handle to Window intId//Hot Key Identifier ); [Flags ()] Public enumkeymodifiers {None=0, Alt=1, Control=2, Shift=4, Windows=8 } }
Register the hotkey first in the Load event in form
/// <summary> ///Register Hotkey/// </summary> /// <param name= "C" >Key</param> /// <param name= "bCTRL" >do you need ctrl</param> /// <param name= "Bshift" >Do you need shift</param> /// <param name= "Balt" >Do you need alt</param> /// <param name= "bwindows" >If you need win</param> Public voidSetHotKey (Keys C,BOOLbCTRL,BOOLBshift,BOOLBalt,BOOLbwindows) { //Assign to variables firstKeys M_key =C; BOOLM_ctrlhotkey =bCTRL; BOOLM_shifthotkey =Bshift; BOOLM_althotkey =Balt; BOOLM_winhotkey =bwindows; //Registration System HotkeyNativewin32.keymodifiers modifiers =NativeWIN32.KeyModifiers.None; if(bCTRL) modifiers|=NativeWIN32.KeyModifiers.Control; if(bshift) modifiers|=NativeWIN32.KeyModifiers.Shift; if(Balt) modifiers|=NativeWIN32.KeyModifiers.Alt; if(bwindows) Modifiers|=NativeWIN32.KeyModifiers.Windows; Nativewin32.registerhotkey (Handle, -, modifiers, c); }
Then listen for messages, handle events
/// <summary> ///overriding Windows message responses/// </summary> /// <param name= "M" ></param> protected Override voidWndProc (refMessage m) { Const intWmhotkey =0x0312; Switch(m.msg) { CaseWmhotkey:windowmax (); Break; } Base. WndProc (refm); }