C # system hotkeys

Source: Internet
Author: User

C # does not set the system hotkey. You need to call the system API.

I found a code segment on the Internet and practiced it well. I recorded it for future use.

The HotKey class code is as follows:

Using System; using System. collections. generic; using System. text; using System. collections; using System. windows. forms; using System. runtime. interopServices; public class Hotkey: IMessageFilter {public delegate void HotkeyEventHandler (int HotKeyID); public event HotkeyEventHandler OnHotkey; private Hashtable keyIDs = new Hashtable (); private IntPtr hWnd; /// <summary> /// secondary key /// </summary> public enu M KeyFlags {MOD_NULL = 0x0, MOD_ALT = 0x1, MOD_CONTROL = 0x2, MOD_SHIFT = 0x4, MOD_WIN = 0x8} // <summary> // register the hotkey API // </summary> [DllImport ("user32.dll")] public static extern UInt32 uregisterhotkey (IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk ); /// <summary> /// cancel the hotkey API /// </summary> [DllImport ("user32.dll")] public static extern UInt32 UnregisterHotKey (IntPtr hWnd, UInt32 id );/ // <Summary> // Add an atom to the global atomic table // </summary> [DllImport ("kernel32.dll")] public static extern UInt32 GlobalAddAtom (String lpString ); /// <summary> /// delete an atom from the global atomic table /// </summary> [DllImport ("kernel32.dll")] public static extern UInt32 GlobalDeleteAtom (UInt32 nAtom ); /// <summary> /// constructor /// </summary> /// <param name = "hWnd"> current handle </param> public Hotkey (IntPtr hWnd) {this. hWnd = hWnd; Application. addMes SageFilter (this);} // <summary> // register the hotkey // </summary> public int RegisterHotkey (Keys Key, KeyFlags keyflags) {UInt32 hotkeyid = GlobalAddAtom (System. guid. newGuid (). toString (); RegisterHotKey (IntPtr) hWnd, hotkeyid, (UInt32) keyflags, (UInt32) Key); keyIDs. add (hotkeyid, hotkeyid); return (int) hotkeyid;} // <summary> // cancel all hotkeys /// </summary> public void UnregisterHotkeys () {Application. remov EMessageFilter (this); foreach (UInt32 key in keyIDs. values) {UnregisterHotKey (hWnd, key); GlobalDeleteAtom (key );}} /// <summary> /// Message filtering /// </summary> public bool PreFilterMessage (ref Message m) {if (m. msg = 0x312) {if (OnHotkey! = Null) {foreach (UInt32 key in keyIDs. values) {if (UInt32) m. WParam = key) {OnHotkey (int) m. WParam); return true ;}}} return false ;}}

Call method:

Private Hotkey hotkey; private int hotKey_Ctrl_F2; private void btnHotKey_Click (object sender, EventArgs e) {if (btnHotKey. text = "register hotkey") {Hotkey = new hotkey (this. handle); // defines the hotkey (Ctrl + F2) hotKey_Ctrl_F2 = hotkey. registerHotkey (System. windows. forms. keys. f2, Hotkey. keyFlags. MOD_CONTROL); hotkey. onHotkey + = new Hotkey. hotkeyEventHandler (OnHotkey); btnHotKey. text = "logout hotkey";} else {hotkey. unregisterHotkeys (); btnHotKey. text = "register hotkey" ;}} private void OnHotkey (int HotkeyID) {if (HotkeyID = hotKey_Ctrl_F2) {this. windowState = FormWindowState. normal; this. focus (); MessageBox. show ("Ctrl + F2 ");}}

Instance download: http://files.cnblogs.com/zjfree/HotKey.rar

Running Environment: WIN2003 + VS2005

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.