1 Public Partial Class Form2: Form 2 { 3 [Dllimport ( " USER32 " )] 4 Public Static Extern Bool Registerhotkey (intptr hwnd, Int ID, Uint Control, keys VK ); 5 // API for removing registration hotkeys 6 [Dllimport ( " USER32 " )] 7 Public Static Extern Bool Unregisterhotkey (intptr hwnd, Int ID ); 8 Const Int Wm_hotkey = Zero X 0312 ; 9 Public Form2 () 10 { 11 Initializecomponent (); 12 } 13 14 Private Void Form2_load ( Object Sender, eventargs E) 15 { 16 // Code 100 17 Hotkey. registerhotkey (handle, 100 , Hotkey. keymodifiers. Ctrl, keys. space ); 18 } 19 20 Private Void Form1_formclosed ( Object Sender, formclosedeventargs E) 21 { 22 // Code 100 23 Hotkey. unregisterhotkey ( This . Handle, 100 ); 24 } 25 26 Protected Override Void Wndproc ( Ref Message m) 27 { 28 Switch (M. msg) 29 { 30 Case Wm_hotkey: 31 { 32 Switch (M. wparam. toint32 ()) 33 { 34 Case 100 : 35 { 36 Sendkeys. Send ( " ^ + " ); // Shift: + ctrl: ^ 37 Break ; 38 } 39 } 40 Break ; 41 } 42 } 43 Base . Wndproc ( Ref M ); 44 } 45 } 46 47 Public Class Hotkey 48 { 49 // If the function is successfully executed, the return value is not 0. 50 // If the function fails to be executed, the return value is 0. To get the extended error message, call getlasterror. 51 [Dllimport ( " User32.dll " , Setlasterror = True )] 52 Public Static Extern Bool Registerhotkey ( 53 Intptr hwnd, // Handle of the window to define the Hot Key 54 Int ID, // Define the hotkey ID (cannot be the same as other IDS) 55 Keymodifiers fsmodifiers, // Indicates whether the hotkey takes effect only when you press ALT, Ctrl, shift, or windows. 56 Keys VK // Define the content of the hotkey 57 ); 58 59 [Dllimport ( " User32.dll " , Setlasterror = True )] 60 Public Static Extern Bool Unregisterhotkey ( 61 Intptr hwnd, // Handle of the window to which the hotkey is to be canceled 62 Int ID // ID of the hotkey to be canceled 63 ); 64 65 // Defines the name of the secondary key (convert a number into a character for memory, or use a value directly for this enumeration) 66 [Flags ()] 67 Public Enum Keymodifiers 68 { 69 None = 0 , 70 Alt = 1 , 71 CTRL = 2 , 72 Shift = 4 , 73 Windowskey = 8 74 } 75 }