C # Introduction to custom shortcut keys

Source: Internet
Author: User
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

 
 
  1. UsingSystem;
  2. UsingSystem. Collections. Generic;
  3. UsingSystem. componentmodel;
  4. UsingSystem. Data;
  5. UsingSystem. drawing;
  6. UsingSystem. text;
  7. UsingSystem. Windows. forms;
  8.  
  9. // To use the dllimport statement, the namespace must be referenced.
  10. UsingSystem. runtime. interopservices;
  11.  
  12. // To use the process statement, the namespace must be referenced.
  13. UsingSystem. diagnostics;
  14. NamespaceWindowsapplication4
  15. {
  16. PublicPartialClassForm1: Form
  17. {
  18.  
  19. // User32.dll is a non-hosted code and cannot be directly referenced using a namespace,
  20. // You need to use "dllimport" before using it.
  21. [Dllimport ("user32.dll", setlasterror =True)]
  22. Public Static Extern BoolRegisterhotkey (
  23. Intptr hwnd, // handle of the window for which the hotkey is to be defined
  24. IntID, // define the hotkey ID (cannot be the same as other IDS)
  25. // Indicates whether the hotkey takes effect only when you press ALT, Ctrl, shift, or windows.
  26. Keymodifiers fsmodifiers,
  27. Keys VK // defines the content of the Hot Key
  28. );
  29. [Dllimport ("user32.dll", setlasterror =True)]
  30. Public Static Extern BoolUnregisterhotkey (
  31. Intptr hwnd, // handle of the window to which the hotkey is to be canceled
  32. IntId // ID of the hotkey to be canceled
  33.  
  34. );
  35. // Defines the name of the secondary key (convert a number into a character for memory, or use a value directly for this enumeration)
  36. [Flags ()]
  37. Public EnumKeymodifiers
  38. {
  39. None = 0,
  40. Alt = 1,
  41. CTRL = 2,
  42. Shift = 4,
  43. Windowskey = 8,
  44. Ctrlandshift = 6
  45. }
  46.  
  47.  
  48. Private VoidForm1_load (ObjectSender, eventargs E)
  49. {
  50. // Register the hotkey SHIFT + s with the ID 100. Keymodifiers. Shift can also be expressed directly by number 4.
  51. Registerhotkey (handle, 100, keymodifiers. Shift, keys. s );
  52. // Register the hotkey Ctrl + B with the ID 101. Keymodifiers. CTRL can also be expressed directly by number 2.
  53. Registerhotkey (handle, 101, keymodifiers. Ctrl, keys. B );
  54. // Register the hot key Alt + D with the ID 102. Keymodifiers. ALT can also be expressed by the number 1.
  55. Registerhotkey (handle, 102, keymodifiers. ALT, keys. D );
  56. // Register the hotkey CTRL + ALT + 0 with the ID 103. Keymodifiers. ctrlandalt can also be expressed directly by the number 3.
  57. Registerhotkey (handle, 103, keymodifiers. ctrlandshift, keys. D0 );
  58. }
  59.  
  60.  
  61. Private VoidForm1_formclosing (ObjectSender, formclosingeventargs E)
  62. {
  63. // Deregister the hotkey setting with ID 100
  64. Unregisterhotkey (handle, 100 );
  65. // Deregister the hotkey setting with ID 101
  66. Unregisterhotkey (handle, 101 );
  67. // Deregister the hotkey setting with ID 102
  68. Unregisterhotkey (handle, 102 );
  69. // Deregister the hotkey setting with ID 103
  70. Unregisterhotkey (handle, 103 );
  71. }
  72. Protected Override VoidWndproc (RefMessage m)
  73. {
  74. Const IntWm_hotkey = 0x0312;
  75. // Press the shortcut key
  76. Switch(M. msg)
  77. {
  78. CaseWm_hotkey:
  79. Switch(M. wparam. toint32 ())
  80. {
  81. Case100: // SHIFT + S
  82. // Enter the shortcut key response code.
  83. Break;
  84. Case101: // press Ctrl + B.
  85. // Enter the shortcut key response code.
  86. Break;
  87. Case102: // press Alt + D.
  88. // Enter the shortcut key response code.
  89. Break;
  90. Case103: // press Ctrl + Shift + 0
  91. {
  92. Process myprocess;
  93. Try
  94. {
  95. // This program function: press Ctrl + Shift + 0 to display the Desktop
  96. Myprocess =NewSystem. Diagnostics. Process ();
  97. Myprocess. startinfo. filename = @ "C: \ showdesktop. SCF ";
  98. Myprocess. startinfo. verb = "open ";
  99. Myprocess. Start ();
  100. }
  101. Catch(Exception ex)
  102. {
  103. // Message displayed when a program error occurs
  104. MessageBox. Show (
  105. Ex. message, "message prompt! ", Messageboxbuttons. OK, messageboxicon. information );
  106. }
  107. Break;
  108. }
  109. }
  110. Break;
  111. }
  112. Base. Wndproc (RefM );
  113. }
  114.  
  115. PublicForm1 ()
  116. {
  117. Initializecomponent ();
  118. }
  119. }
  120. }

Through the above code, you can set the C # custom shortcut key. You can try it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.