The entire process of using the system hotkey in the C # program.

Source: Internet
Author: User
1. First introduce system. runtime. interopservices

Using system. runtime. interopservices;

2. Declare two API functions in the class. Their positions are the same as those of the class member variables.

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

3. Define an enumeration of keymodifiers to enable a key combination.

Public Enum keymodifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}

4. register the system hotkey in the class Constructor

For example, the following example registers four hotkeys:

Public mainform ()
{
Initializecomponent ();

Registerhotkey (handle, 100, 2, keys. Left); // hotkey 1: Control + cursor left arrow
Registerhotkey (handle, 200, 2, keys. Right); // hotkey 1: Control + right arrow of the cursor
Registerhotkey (handle, 300, 2, keys. Up); // hotkey 1: Control + cursor up arrow
Registerhotkey (handle, 400, 2, keys. Down); // hotkey 1: Control + cursor down arrow

....;
}

5. Rewrite the wndproc () method and call the process by monitoring system messages.

Example:

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.
}

5. Needless to say, we need to implement the processhotkey function:

// Call this function when you press the specified key.

Private void processhotkey (message m)
{
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": decresevolumnb (); break; // press the Control + left arrow of the cursor to call the decresevolumnb () function ();
Case "200": addvolumnb (); break; // Press Control + the right arrow of the cursor to call the addvolumnb () function ()
Case "300": // press the Control + cursor arrow to display the form
This. Visible = true;
Break;
Case "400": // press the Control + cursor down arrow to hide the form
This. Visible = false;
Break;
}
}

Obviously, the following functions are implemented respectively: decresevolumnb (); and addvolumnb.

6. Don't forgetProgramCancel hotkey registration upon exit

Private void mainform_formclosing (Object sender, formclosingeventargs E)
{
Unregisterhotkey (handle, 100); // uninstall the 1st shortcut keys
Unregisterhotkey (handle, 200); // contains 2nd shortcut keys
Unregisterhotkey (handle, 300); // uninstall the 3rd shortcut keys
Unregisterhotkey (handle, 400); // contains 4th shortcut keys
}

The above is the whole process of using the system hotkey in the C # program.

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.