First reference the namespace of using system. runtime. interopservices,
Add the following in the appropriate position: Code OK .. Note: form1_load and form1_formclosed cannot be copied directly ~
Copy code The Code is as follows: [dllimport ("USER32")]
Public static extern bool registerhotkey (intptr hwnd, int ID, uint control, keys VK );
// Register the hotkey API
[Dllimport ("USER32")]
Public static extern bool unregisterhotkey (intptr hwnd, int ID );
Private void form1_load (Object sender, eventargs E)
{
// Register the hotkey (Form handle, hotkey ID, secondary key, and real key)
Registerhotkey (this. Handle, 888, 2, keys. );
}
Private void form=formclosed (Object sender, formclosedeventargs E)
{
// Delete the hotkey (handle, hotkey ID)
Unregisterhotkey (this. Handle, 888 );
}
Protected override void wndproc (ref message m)
{
Switch (M. msg)
{
Case 0x0312: // This is the registered hotkey message defined by the window message.
If (M. wparam. tostring (). Equals ("888") // if it is the hotkey we registered
MessageBox. Show ("You press Ctrl + ");
Break;
}
Base. wndproc (ref m );
}
Auxiliary key description:
None = 0,
Alt = 1,
Crtl = 2,
Shift = 4,
Windows = 8
If there are multiple secondary keys, for example, ALT + crtl is 3 and can be directly added.