C # register the global hotkey (register hot key)

Source: Internet
Author: User

It is easy to register A method similar to ctr + alt + shit + A + Z. Set the 3rd parameters of RegisterHotKey to KeyModifiers. Alt | KeyModifiers. Control | KeyModifiers. Shift,
Set the 4th parameters to Keys. B | Keys. Z.
 
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Runtime. InteropServices;
Using System. Threading;
Namespace rgHotKeys
{
Public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
Public partial class Form1: Form
{
[DllImport ("user32.dll", SetLastError = true)]
Public static extern bool RegisterHotKey (IntPtr hwnd, int id, int fsModifiers, int vk );
[DllImport ("user32.dll", SetLastError = true)]
Public static extern bool UnregisterHotKey (
IntPtr hWnd, // handle to window
Int id // hot key identifier
);
Private int id;
Public Form1 ()
{
InitializeComponent ();
}
Private void Form1_Load (object sender, EventArgs e)
{
Id = Thread. CurrentThread. GetHashCode ();
RegisterHotKey (this. Handle, id, (int) KeyModifiers. Alt, (int) Keys. F12 );
}
Protected override void WndProc (ref Message m)
{
Const int WM_HOTKEY = 0x0312;
Switch (m. Msg)
{
Case WM_HOTKEY:
If (id = (int) m. WParam)
{
System. Windows. Forms. MessageBox. Show ("Hello! ");
}
Break;
}
Base. WndProc (ref m );
}
Private void form=formclosed (object sender, FormClosedEventArgs e)
{
UnregisterHotKey (this. Handle, 10001 );
}
}
}
 

Related Article

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.