ArticleDirectory
- This article uses pressing CTRL + Shift + 0 to display the desktop as an example. The program code written in C # is used to describe the implementation of C # custom shortcut keys.
This article uses pressing CTRL + Shift + 0 to display the desktop as an example. Program Code Describes the implementation of C # custom shortcut keys.
Readers can follow these steps to implement some custom functions by pressing some keys. You only need to modify the parameters of registerhotkey in the following code and the execution content in the case statement.
The key points in the example program below are annotated.
The following is a complete and runable example program written in C #.
Open the integrated development environment vs2005 and create a new windows application. The following is all the code of form1.cs.
(Note: to make the program run correctly, you must replace c: \ showdesktop. SCF in the following code with the path of your local "display desktop. SCF" file)
C # custom shortcut key implementation code
-
- UsingSystem;
-
- UsingSystem. Collections. Generic;
- UsingSystem. componentmodel;
-
- UsingSystem. Data;
-
- UsingSystem. drawing;
-
- UsingSystem. text;
-
- UsingSystem. Windows. forms;
-
-
-
- // To use the dllimport statement, the namespace must be referenced.
-
- UsingSystem. runtime. interopservices;
-
-
-
- // To use the process statement, the namespace must be referenced.
-
- UsingSystem. diagnostics;
-
-
- NamespaceWindowsapplication4
-
- {
-
- PublicPartialClassForm1: Form
-
- {
-
-
- // User32.dll is a non-hosted code and cannot be directly referenced using a namespace,
-
- // You need to use "dllimport" before using it.
-
- [Dllimport ("user32.dll", setlasterror =True)]
-
- Public Static Extern BoolRegisterhotkey (
-
- Intptr hwnd, // handle of the window for which the hotkey is to be defined
-
- IntID, // define the hotkey ID (cannot be the same as other IDS)
-
- // Indicates whether the hotkey takes effect only when you press ALT, Ctrl, shift, or windows.
-
- Keymodifiers fsmodifiers,
-
- Keys VK // defines the content of the Hot Key
-
- );
-
-
- [Dllimport ("user32.dll", setlasterror =True)]
- Public Static Extern BoolUnregisterhotkey (
-
- Intptr hwnd, // handle of the window to which the hotkey is to be canceled
-
- IntId // 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 EnumKeymodifiers
-
- {
-
- None = 0,
-
- Alt = 1,
-
- CTRL = 2,
-
- Shift = 4,
-
- Windowskey = 8,
-
- Ctrlandshift = 6
-
- }
-
-
-
-
- Private VoidForm1_load (ObjectSender, eventargs E)
-
- {
-
- // Register the hotkey SHIFT + s with the ID 100. Keymodifiers. Shift can also be expressed directly by number 4.
-
- Registerhotkey (handle, 100, keymodifiers. Shift, keys. s );
-
- // Register the hotkey Ctrl + B with the ID 101. Keymodifiers. CTRL can also be expressed directly by number 2.
-
- Registerhotkey (handle, 101, keymodifiers. Ctrl, keys. B );
-
- // Register the hot key Alt + D with the ID 102. Keymodifiers. ALT can also be expressed by the number 1.
-
- Registerhotkey (handle, 102, keymodifiers. ALT, keys. D );
-
- // Register the hotkey CTRL + ALT + 0 with the ID 103. Keymodifiers. ctrlandalt can also be expressed directly by the number 3.
-
- Registerhotkey (handle, 103, keymodifiers. ctrlandshift, keys. D0 );
-
- }
-
-
-
-
- Private VoidForm1_formclosing (ObjectSender, formclosingeventargs E)
-
- {
-
- // Deregister the hotkey setting with ID 100
-
- Unregisterhotkey (handle, 100 );
-
- // Deregister the hotkey setting with ID 101
-
- Unregisterhotkey (handle, 101 );
-
- // Deregister the hotkey setting with ID 102
-
- Unregisterhotkey (handle, 102 );
-
- // Deregister the hotkey setting with ID 103
-
- Unregisterhotkey (handle, 103 );
-
- }
-
-
- Protected Override VoidWndproc (RefMessage m)
-
- {
- Const IntWm_hotkey = 0x0312;
-
- // Press the shortcut key
-
- Switch(M. msg)
-
- {
-
- CaseWm_hotkey:
-
- Switch(M. wparam. toint32 ())
-
- {
-
- Case100: // SHIFT + S
-
- // Enter the shortcut key response code.
-
- Break;
-
- Case101: // press Ctrl + B.
-
- // Enter the shortcut key response code.
-
- Break;
-
- Case102: // press Alt + D.
-
- // Enter the shortcut key response code.
-
- Break;
- Case103: // press Ctrl + Shift + 0
-
- {
-
- Process myprocess;
-
- Try
-
- {
-
- // This program function: press Ctrl + Shift + 0 to display the Desktop
-
- Myprocess =NewSystem. Diagnostics. Process ();
-
- Myprocess. startinfo. filename = @ "C: \ showdesktop. SCF ";
-
- Myprocess. startinfo. verb = "open ";
-
- Myprocess. Start ();
-
- }
-
- Catch(Exception ex)
-
- {
-
- // Message displayed when a program error occurs
-
- MessageBox. Show (
-
- Ex. message, "message prompt! ", Messageboxbuttons. OK, messageboxicon. information );
- }
-
- Break;
-
- }
-
- }
-
- Break;
-
- }
-
- Base. Wndproc (RefM );
-
- }
-
-
-
- PublicForm1 ()
-
- {
-
- Initializecomponent ();
-
- }
-
- }
-
- }
Through the above code, you can set the C # custom shortcut key. You can try it.