使用函数:GetAsyncKeyState
函数声明:Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Long
函数说明:
返回值 Long,自对GetAsyncKeyState函数的上一次调用以来,如键已被按过,则位0设为1;否则设为0。如键目前处于按下状态,则位15设为1;如抬起,则为0。微软的win32手册指出:倘若输入焦点从属于与调用函数的输入线程不同的另一个输入线程,则返回值为0(例如,一旦另一个程序拥有焦点,则它应返回零),但是证据显示,函数实际是在整个系统的范围内工作的,也就是说,只要这个函数没被hook掉,都可以取到键的状态,并且不受各类热键影响。
使用实例:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Long
//测试全局热键ctrl+alt+del
Do
Dim ctrl, alt, del
ctrl = GetAsyncKeyState(17)
If ctrl = 0 Then
ctrl = GetAsyncKeyState(162)
End If
alt = GetAsyncKeyState(18)
If alt = 0 Then
alt = GetAsyncKeyState(164)
End If
del = GetAsyncKeyState(110)
If del = 0 Then
del = GetAsyncKeyState(46)
End If
If ctrl and alt and del Then
TracePrint time & "已响应ctrl+alt+del!"
Delay 500
End If
Delay 200
Loop
//测试读取脚本启动热键F10的状态:
Do
Dim 结果
结果 = GetAsyncKeyState(121)
If 结果 Then
TracePrint time & "已按下/开始检测前被按下过!"
Delay 500
Else
TracePrint time & "已弹起!"
End If
Delay 200
Loop