Q:winform How do I register a hotkey?
A:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.DirectoryServices;
Using System.Runtime.InteropServices; Namespace Test ... {public partial class form1:form ... {Class HotKey ...
{//If the function succeeds, the return value is not 0. If the function fails to execute, the return value is 0.
To get extended error messages, call GetLastError.
[DllImport ("user32.dll", SetLastError = true)] public static extern bool RegisterHotKey (
IntPtr hWnd,//the handle int ID of the window to define the hotkey,//define the Hotkey ID (cannot repeat with other IDs) Keymodifiers fsmodifiers,//identifies whether the hotkey will take effect when pressing ALT, Ctrl, Shift, windows, etc. keys VK
Define the contents of the hotkey);
[DllImport ("user32.dll", SetLastError = true)] public static extern bool Unregisterhotkey ( INtptr hWnd,//the handle int ID of the window to cancel the hotkey//ID to cancel the hotkey);
Defines the name of a secondary key (converts a number to characters to facilitate memory, but also eliminates this enumeration and uses numeric values directly) [Flags ()] public enum Keymodifiers ...
{None = 0, Alt = 1, Ctrl = 2, Shift = 4, WindowsKey = 8}} public Form1 () ...
{InitializeComponent (); Hotkey.registerhotkey (this.
Handle, 0, KEYS.F1); } 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:messagebox.show ("Press F1");
Break } break; Base.
WndProc (ref m); } } }