这里是声明部分
'* 操作键盘 Hook 的代码
Private Const WM_HOTKEY As Integer = &H312
Private Const MOD_ALT As Integer = &H1
Private Const MOD_CONTROL As Integer = &H2
Private Const MOD_SHIFT As Integer = &H4
Private Const GWL_WNDPROC As Integer = (-4)
Private 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
Private Declare Auto Function UnRegisterHotKey Lib "user32.dll" Alias "UnregisterHotKey" (ByVal hwnd As IntPtr, ByVal id As Integer) As Boolean
Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As IntPtr) As Integer
这里是全局的方法
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_HOTKEY Then
'* 激活当前窗体
SetForegroundWindow(Me.Handle)
End If
MyBase.WndProc(m)
End Sub
这里是注册键盘 hook 的过程
'* 注册键盘 Hook 如果有返回值则返回注册成功或失败的 Boolean
'* 32 是 空格键,该操作是注册 shift+space 达到快速切换到当前窗口的目的
If Not RegisterHotKey(Handle, 0, MOD_SHIFT, 32) Then
Throw New Exception("Mini Mickey 的 RegisterHotKey 失败")
End If
这里是解除注册的过程
'* 删除键盘 Hook 的绑定,如果有返回值则返回删除成功或失败的 Boolean
UnRegisterHotKey(Handle, 0)