Option Explicit
' 声明API函数
Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long
' 声明结构
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type Msg
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
' 声明常数
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private HotKey_Flg As Boolean
Private Sub Form_Load()
Dim Message As Msg
'注册 Ctrl+Shift+I 为热键
RegisterHotKey Me.hWnd, &HBFFF&, MOD_CONTROL + MOD_SHIFT, vbKeyI
Me.Show
'等待处理消息
HotKey_Flg = False
Do While Not HotKey_Flg
'等待消息
WaitMessage
'检查是否热键被按下
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
'打开计算器程序
Shell "calc.exe", vbNormalFocus
End If
'转让控制权,允许操作系统处理其他事件
DoEvents
Loop
End Sub
Private Sub Form_Unload(Cancel As Integer)
HotKey_Flg = True
'撤销热键的注册
Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub