C # System Hotkey Registration Implementation Code _c# Tutorial

Source: Internet
Author: User
Reference using System.Runtime.InteropServices first; The namespaces,
Then add the following code in the appropriate location to OK. Note: Form1_Load and form1_formclosed can not be directly copy Oh ~
Copy Code code as follows:

[DllImport ("user32")]
public static extern bool RegisterHotKey (IntPtr hwnd,int id,uint control,keys VK);
Registering the Hotkey API
[DllImport ("user32")]
public static extern bool Unregisterhotkey (INTPTR hWnd, int id);

private void Form1_Load (object sender, EventArgs e)
{
Register hotkey (form handle, Hotkey ID, auxiliary key, real key)
RegisterHotKey (this. Handle, 888, 2, KEYS.A);
}
private void Form1_formclosed (object sender, Formclosedeventargs e)
{
Injection 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 for the window message definition
if (m.wparam.tostring (). Equals ("888"))//If the hot key we registered for
MessageBox.Show ("You pressed CTRL + a");
Break
}
Base. WndProc (ref m);
}

Secondary key Description:
None = 0,
Alt = 1,
Crtl= 2,
Shift = 4,
Windows = 8
If you have more than one auxiliary key, for example, Alt+crtl is 3 to add directly.

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.