Set shortcut problem in C #

xiaoxiao2021-03-31  200

I have recently been found, which is to set the shortcut operation method or program in C #.

To set shortcuts, you must use the two methods below User32.dll.

Bool RegisterhotKey (HWND HWND, INT ID, UINT FSMODIFIERS, UINT VK);

with

Bool UnregisterhotKey (HWND HWND, INT ID); converts into C # code, then first reference the namespace system.Runtime.InteropServices; to load the unmanaged user32.dll. So there is:

[DllImport ( "user32.dll", SetLastError = true)] public static extern bool RegisterHotKey (IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options Keys vk // virtual -Key code);

[DLLIMPORT ("User32.dll", setLastError = true)] public static extern bool unregisterhotkey (INTPTR HWND, // Handle to Window Int ID // Hot Key Identifier);

[Flags ()] public enum keymodifiers {none = 0, alt = 1, control = 2, shift = 4, windows = 8}

This is a method of registration and uninstalling global shortcuts, then we only need to add a registration shortcut when form_load, uninstall the global shortcut time when formclosing. At the same time, in order to ensure that the contents of the clipboard are not subject to other programs to call the clipboard interference, when Form_Load, I first empty the contents of the clipboard.

So there is:

Private Void Form1_Load (Object Sender, System.Eventargs E) {label2.autosize = true;

Clipboard.clear (); // First empty the clipboard to prevent the clipboard from copying other content in registerhotKey (Handle, 100, 0, Keys.f10);

PRIVATE VOID FORM1_FORMCLOSING (Object sender, formclosingeventargs e) {unregisterhotkey (Handle, 100); // Uninstall shortcut}

Then we are in other windows, how to call my main procedure processhotKey () after pressing the shortcuts?

Then we must rewrite the WndProc () method, call the process by monitoring the system message:

Protected Override Void WndProc (Ref Message M) // Monitor Windows Message {Const Int WM_HOTKEY = 0x0312; // Press Specical Key Switch (M.MSG) {Case WM_HOTKEY: ProcesshotKey (); // Call the main handler Break;} Base .Wndproc (ref m);}

So my program is completed.

The number key is Alt Num0 ~ Num9 combination password key is the Alt keypad's plus query button is the Alt mini button back key is the Alt keypad aspect, the keyboard is the Alt keypad / number left direction button is Shift keypad / number The right arrow keys are Shift keypad aspect

转载请注明原文地址:https://www.9cbs.com/read-130802.html

New Post(0)