Call the API function registerhotkey in C # To register multiple system hotkeys.

Source: Internet
Author: User

To set the shortcut key, you must use the two methods below user32.dll.
Bool registerhotkey (// register the system hotkey API Function
Hwnd,
Int ID,
Uint fsmodifiers,
Uint VK
);
 
Bool unregisterhotkey (// API function used to delete the system hotkey
Hwnd,
Int ID
);
 
REFERENCE The namespace system. runtime. interopservices; in C # to load the unmanaged class user32.dll.
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
 
Namespace hotkey
{
 
Public Enum keymodifiers // key combination Enumeration
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
 
Public partial class form1: Form
{
/*
* Registerhotkey function prototype and description:
* Bool registerhotkey (
* Hwnd, // window to receive hot-key notification
* Int ID, // identifier of Hot Key
* Uint fsmodifiers, // key-modifier flags
* Uint VK // virtual-key code );
* Parameter ID is an ID value defined for you
* For a thread, the value must be in the range of 0x0000-0xbfff, And the decimal value is 0 ~ 49151
* For DLL, the value must be in the range of 0xc000-0 xFFFF, And the decimal value is 49152 ~ 65535
* In the same process, this value must be unique. The fsmodifiers parameter specifies the key used together with the hotkey.
* Optional values: mod_alt mod_control mod_win mod_shift, or 0: none; 1: ALT; 2: control; 4: shift; 8: Windows
* VK indicates the virtual key code of the hotkey.
*/
 
[System. runtime. interopservices. dllimport ("user32.dll")] // declare an API Function
Public static extern bool registerhotkey (
Intptr hwnd, // handle to window
Int ID, // Hot Key Identifier
Uint fsmodifiers, // key-modifier options
Keys VK // virtual-key code
);
[System. runtime. interopservices. dllimport ("user32.dll")] // declare an API Function
Public static extern bool unregisterhotkey (
Intptr hwnd, // handle to window
Int ID // Hot Key Identifier
);
Public form1 ()
{
Initializecomponent ();
}
Private void processhotkey (message m) // call this function when you press the set key.
{
Intptr id = M. wparam; // intptr indicates the platform-specific type of the pointer or handle.
// MessageBox. Show (Id. tostring ());
String SID = ID. tostring ();
Switch (SID)
{
Case "100 ":
MessageBox. Show ("call function ");
Break;
Case "200 ":
MessageBox. Show ("call function B ");
Break;
}
}
Private void form1_load (Object sender, eventargs E)
{
// Handle is the handle of the current window, and continues from control. Handle. Control is the base class of the control defined.
// Registerhotkey (handle, 100, 0, keys. a); // register the shortcut key. The hot key is.
// Registerhotkey (handle, 100, keymodifiers. Alt | keymodifiers. Control, keys. B); // The Hot Key is Alt + Ctrl + B.
// Registerhotkey (handle, 100, 1, keys. B); // 1 is the Alt key and the hot key is Alt + B
Registerhotkey (handle, 100, 2, keys. a); // defines the hot key as ALT + TAB. Here the Alt + Tab key of the screen system is implemented.
Registerhotkey (handle, 200, 2, keys. B); // register two hotkeys and determine which function to execute Based on the ID value 100,200.
}
 
Private void button#click (Object sender, eventargs e) // reset the hotkey
{
Unregisterhotkey (handle, 100); // uninstall the shortcut key
Registerhotkey (handle, 100, 2, keys. c); // register a new shortcut key. parameter 0 indicates no combination key.
}
 
Private void form=formclosing (Object sender, formclosingeventargs e) // The hotkey is loaded when the program exits.
{
Unregisterhotkey (handle, 100); // uninstall the 1st shortcut keys
Unregisterhotkey (handle, 200); // contains 2nd shortcut keys
}
 
// Rewrite the wndproc () method to call the process by monitoring system messages
Protected override void wndproc (ref message m) // monitor Windows messages
{
Const int wm_hotkey = 0x0312; // if the value of M. MSG is 0x0312, the user presses the hot key.
Switch (M. msg)
{
Case wm_hotkey:
Processhotkey (m); // call the processhotkey () function when the hot key is pressed.
Break;
}
Base. wndproc (ref m); // transmits the system message from the wndproc of the parent class.
}
}
}

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.