WinForm receive global shortcut keys

Source: Internet
Author: User

 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); }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.