In applications, we may need to implement shortcut keys such as Ctrl + C copy and Ctrl + V paste. This article briefly introduces its implementation and provides an implementation class.
(1) create a class file named HotKey. cs. The Code is as follows:
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Runtime. InteropServices;
Using System. Windows. Forms;
Namespace KoalaStudio. BookshopManager
{
Class HotKey
{
// If the function is successfully executed, the return value is not 0.
// If the function fails to be executed, the return value is 0. To get the extended error message, call GetLastError.
[DllImport ("user32.dll", SetLastError = true)]
Public static extern bool RegisterHotKey (
IntPtr hWnd, // handle of the window for which the hotkey is to be defined
Int id, // define the hotkey ID (cannot be the same as other IDS)
KeyModifiers fsModifiers, // identifies whether the hot key takes effect only when you press Alt, Ctrl, Shift, or Windows.
Keys vk // defines the content of the Hot Key
);
[DllImport ("user32.dll", SetLastError = true)]
Public static extern bool UnregisterHotKey (
IntPtr hWnd, // handle of the window to which the hotkey is to be canceled
Int id // ID of the hotkey to be canceled
);
// Defines the name of the secondary key (convert a number into a character for memory, or use a value directly for this enumeration)
[Flags ()]
Public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}
}
Brief description:
The "public static extern bool RegisterHotKey ()" function is used to register hotkeys. This function can be used only after the dynamic link library of user32.dll is referenced.
User32.dll is a non-hosted code and cannot be directly referenced in the namespace. Therefore, you need to use "DllImport" before using it. Therefore, you need to add
"[DllImport (" user32.dll ", SetLastError = true.
The "public static extern bool UnregisterHotKey ()" function is used to deregister the hotkey. Similarly, you must use DllImport to reference user32.dll before using it.
"Public enum KeyModifiers {}" defines a set of enumerations. The numeric code of the secondary key is directly expressed as text for ease of use. In this way, you do not have to remember the code of every secondary key during the call, but simply select its name.
(2) take the form FormA as an example to introduce how to use the HotKey class to register the HotKey in the Activate event of FormA. In this example, register Shift + S, Ctrl + Z, alt + D. The ID number can be set as needed, but it must not be repeated.
Copy codeThe Code is as follows: private void Form_Activated (object sender, EventArgs e)
{
// Register the hotkey Shift + S with the ID 100. HotKey. KeyModifiers. Shift can also be expressed directly by number 4.
HotKey. RegisterHotKey (Handle, 100, HotKey. KeyModifiers. Shift, Keys. S );
// Register the hotkey Ctrl + B with the ID 101. HotKey. KeyModifiers. Ctrl can also be expressed directly by number 2.
HotKey. RegisterHotKey (Handle, 101, HotKey. KeyModifiers. Ctrl, Keys. B );
// Register the hot key Alt + D with the ID 102. HotKey. KeyModifiers. Alt can also be expressed by the number 1.
HotKey. RegisterHotKey (Handle, 102, HotKey. KeyModifiers. Alt, Keys. D );
}
Unregister the hotkey from the Leave event of FormA.
Copy codeThe Code is as follows: private void FrmSale_Leave (object sender, EventArgs e)
{
// Deregister the hotkey setting with ID 100
HotKey. UnregisterHotKey (Handle, 100 );
// Deregister the hotkey setting with ID 101
HotKey. UnregisterHotKey (Handle, 101 );
// Deregister the hotkey setting with ID 102
HotKey. UnregisterHotKey (Handle, 102 );
}
Reload the WndProc function in FromA.
Copy codeThe Code is as follows :///
/// Monitor Windows messages
/// Reload the WndProc Method for hotkey response
///
///
Protected override void WndProc (ref Message m)
{
Const int WM_HOTKEY = 0x0312;
// Press the shortcut key
Switch (m. Msg)
{
Case WM_HOTKEY:
Switch (m. WParam. ToInt32 ())
{
Case 100: // Shift + S
// Enter the shortcut key response code.
Break;
Case 101: // press Ctrl + B.
// Enter the shortcut key response code.
Break;
Case 102: // Alt + D is pressed
// Enter the shortcut key response code.
Break;
}
Break;
}
Base. WndProc (ref m );
}
After the code is completed, the program responds to any of the three Shortcut Keys Shift + S, Ctrl + B, and Alt + D in the form.