首先,需要声明相关函数及变量,如下:
#Region "声明注册热键API 函数"
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)
Public Declare Auto Function RegisterHotKey Lib "user32.dll" Alias _
"RegisterHotKey" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Boolean
Public Declare Auto Function UnRegisterHotKey Lib "user32.dll" Alias _
"UnregisterHotKey" (ByVal hwnd As IntPtr, ByVal id As Integer) As Boolean
#End Region
之后,就需要调用注册函数,可以将下面的语句添入窗体的 Load 过程:
'注册热键
'Ctrl+Shift+C
RegisterHotKey(Me.Handle, 4414, MOD_CONTROL + MOD_SHIFT, Keys.C)
最后,添加热键响应过程:
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_HOTKEY Then
'热键所要执行的操作
End If
MyBase.WndProc(m)
End Sub