Function
To set a shortcut key, you must use the two methods below user32.dll.
BOOL RegisterHotKey (//Registration system Hotkey API function
HWND hwnd,
int ID,
UINT Fsmodifiers,
UINT VK
);
BOOL Unregisterhotkey (//removal of System Hotkey API function
HWND hwnd,
int ID
);
Referencing namespace System.Runtime.InteropServices in C # to load unmanaged classes 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//combination Key Enumeration
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
public partial class Form1:form
{
/*
* RegisterHotKey function Prototype and description:
* BOOL RegisterHotKey (
* HWND 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 that you define yourself
* For a thread the value must be within the 0X0000-0XBFFF range, the decimal is 0~49151
* The value of the DLL must be within the 0XC000-0XFFFF range, the decimal is 49152~65535
* Within the same process the value must be unique parameter fsmodifiers indicates the use of a key with a hotkey
* The desirable value is: Mod_alt mod_control mod_win mod_shift parameter, or number 0 is none, 1 is alt,2 for the control,4 for Windows
* VK the Virtual key code indicating the hotkey
*/
[System.Runtime.InteropServices.DllImport ("user32.dll")]//Declaration API functions
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")]//Declaration API functions
public static extern bool Unregisterhotkey (
IntPtr hWnd,//Handle to Window
int ID//Hot Key Identifier
);
Public Form1 ()
{
InitializeComponent ();
}
Call this function when private void Processhotkey (message m)//key is pressed
{
INTPTR id = m.wparam; IntPtr the platform-specific type used to represent pointers or handles
MessageBox.Show (ID. ToString ());
string sid = Id. ToString ();
Switch (SID)
{
Case "100":
MessageBox.Show ("Call a function");
Break
Case "200":
MessageBox.Show ("Call B function");
Break
}
}
private void Form1_Load (object sender, EventArgs e)
{
Handle is the handle to the current window and continues to Control.handle,control as the base class for the defined control
RegisterHotKey (Handle, 0, KEYS.A); Register shortcut key, Hotkey is a
RegisterHotKey (Handle, Keymodifiers.alt | Keymodifiers.control, keys.b);/The Hotkey is Alt+ctrl+b
RegisterHotKey (Handle, 1, keys.b); 1 is the ALT key and the hotkey is alt+b
RegisterHotKey (Handle, 2,KEYS.A); Defines a hotkey for ALT + TAB, which implements the Screen System tab key
RegisterHotKey (Handle, 2, keys.b); Register 2 hotkeys and determine which function to perform according to the ID value 100,200
}
private void Button1_Click (object sender, EventArgs e)//Reset Hotkey
{
Unregisterhotkey (Handle, 100);//Uninstall shortcut keys
RegisterHotKey (Handle, 2, KEYS.C); Registers a new shortcut key, parameter 0 indicates no key combination
}
private void Form1_formclosing (object sender, FormClosingEventArgs e)//When exiting program unloading load Hotkey
{
Unregisterhotkey (Handle, 100);//uninstall 1th shortcut key
Unregisterhotkey (Handle, 200); Unloading download 2nd shortcut key
}
Rewrite the WndProc () method to invoke the procedure by monitoring system messages
protected override void WndProc (ref message m)//Monitor Windows messages
{
const int Wm_hotkey = 0x0312;//if M. The value of MSG is 0x0312 so that the user presses the hotkey
Switch (m.msg)
{
Case Wm_hotkey:
Processhotkey (m)//Call Processhotkey () function when the hot key is pressed
Break
}
Base. WndProc (ref m); Passing system messages from the WndProc of the parent class
}
}
}