使用RegisterHotkey的概念是,它会定义一组或单个按键的组合,不管在哪个程序之中,按下程序窗体有注册的HotKey时,系统会传送WM_HOTKEY 的讯息给待接收该讯息的程序窗体,而该程序窗体接收到WM_HOTKEY时,便可知道有本身Thread所定义的HotKey被按下,於是可以从wParam, lParam来得知是哪一组HotKey被按下,从而执行相应的操作.
函数声明的定义如下:
RegisterHotKey(
ByVal hwnd As Long , //接收自定义热键的窗口的HWND
ByVal idHotKey as Long, //id为你自己定义的一个ID值对一个线程来讲其值必需在0x0000 - 0xBFFF范围之内,十进制为0~49151, 对DLL来讲其值必需在0xC000 - 0xFFFF 范围之内,十进制为49152~65535,在同一进程内该值必须唯一参数 fsModifiers指明与热键联合使用按键
ByVal Modifiers As Long, // 指明与热键联合使用按键(ALT,SHIFT,CTR的组合)如不需可置空
,可取值为:MOD_ALT, MOD_CONTROL, MOD_WIN, MOD_SHIFT参数,或数字0为无,1为Alt,2为Control,4为Shift,8为Windows
ByVal uVirtKey As Long //定主你自定的HOTKEY的虚拟按键码
)
WM_HOTKEY 叁数的定义
idHotKey = wParam;
Modifiers = (UINT) LOWORD(lParam);
uVirtKey = (UINT) HIWORD(lParam);
所以了,除了设定RegisterHotkey外,另要使用SubClassing的技巧才会得知HotKey被按
下;最後,程序结束前要使用UnRegisterHotkey将HotKey的定义取消掉。切记,一定要有用有还!!!
以下程序功能是:不管在哪个程序中,只要按下 ALT-SHIFT-G 便执行 NotePad 。
'以下在.Bas
Option Explicit
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
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 preWinProc As Long
Public Modifiers As Long, uVirtKey As Long, idHotKey As Long
Private Type taLong
ll As Long
End Type
Private Type t2Int
lWord As Integer
hword As Integer
End Type
Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, or="#0000FF">ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
Dim lp As taLong, i2 As t2Int
lp.ll = lParam
LSet i2 = lp
If (i2.lWord = Modifiers) And i2.hword = uVirtKey Then
‘此处为接收到热键后需执行的操作
End If
End If
End If
'将之送往原来的Window Procedure
wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
'以下在 Form
Sub Form_Load()
Dim ret As Long
preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf wndproc)
idHotKey = 1 'in the range &h0000 through &hBFFF
Modifiers = MOD_ALT + MOD_SHIFT
uVirtKey = vbKeyG
ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim ret As Long
'取消Message的截取,而使之又只送往原来的Window Procedure
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc)
Call UnregisterHotKey(Me.hwnd, uVirtKey)
End Sub