本帖最后由 3131210 于 2018-12-22 01:20 编辑 ;~ #RequireAdmin
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $LinkKeyList = 'None|Shift|Ctrl|Alt|0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|F1|F2|F3|F4|F5|F6|F7|F8|F9|F10|F11|F12'
Global $KeyCode = '0|16|17|18|48|49|50|51|52|53|54|55|56|57|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|112|113|114|115|116|117|118|119|120|121|122|123'
Global $hHook, $hStub_KeyProc
Global $testkey1 = 'Alt + Q'
Global $testkey2 = 'Shift + S'
Global $testkey3 = 'Shift + A'
Global $testkey4 = 'Shift + D'
$hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
$hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), _WinAPI_GetModuleHandle(0))
Local $hGUI = GUICreate("Demo", 500, 300)
GUICtrlCreateLabel('ESC 退出' & @CRLF & @CRLF & _
$testkey1 & @CRLF & @CRLF & _
$testkey2 & @CRLF & @CRLF & _
$testkey3 & @CRLF & @CRLF & _
$testkey4, 10, 60)
GUISetState(@SW_SHOW)
While 1
Sleep(100)
WEnd
Func _KeyProc($nCode, $wParam, $lParam)
Local $tKEYHOOKS, $wVKey
$tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
If $nCode < 0 Then
Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndIf
$wVKey = DllStructGetData($tKEYHOOKS, "vkCode")
Select
Case $wVKey = 27
Exit
Case $wVKey = _GetKeyAsc(($testkey1), 2) and _CheckKeyStatus($testkey1) = 1
If $wParam = $WM_KEYDOWN Or $wParam = $WM_SYSKEYDOWN Then ;按下
ConsoleWrite($testkey1 & " was down." & @CRLF)
Send('{UP down}')
Return 1
ElseIf $wParam = $WM_KEYUP Or $wParam = $WM_SYSKEYUP Then ;弹起
ConsoleWrite($testkey1 & " was up." & @CRLF)
Send('{UP up}')
EndIf
Case $wVKey = _GetKeyAsc($testkey2, 2) and _CheckKeyStatus($testkey2) = 1
If $wParam = $WM_KEYDOWN Or $wParam = $WM_SYSKEYDOWN Then ;按下
ConsoleWrite($testkey2 & " was down." & @CRLF)
Send('{DOWN down}')
Return 1
ElseIf $wParam = $WM_KEYUP Or $wParam = $WM_SYSKEYUP Then ;弹起
ConsoleWrite($testkey2 & " was up." & @CRLF)
Send('{DOWN up}')
EndIf
Case $wVKey = _GetKeyAsc($testkey3, 2) and _CheckKeyStatus(($testkey3)) = 1
If $wParam = $WM_KEYDOWN Or $wParam = $WM_SYSKEYDOWN Then ;按下
ConsoleWrite($testkey3 & " was down." & @CRLF)
Send('{LEFT down}')
Return 1
ElseIf $wParam = $WM_KEYUP Or $wParam = $WM_SYSKEYUP Then ;弹起
ConsoleWrite($testkey3 & " was up." & @CRLF)
Send('{LEFT up}')
EndIf
Case $wVKey = _GetKeyAsc($testkey4, 2) and _CheckKeyStatus($testkey4) = 1
If $wParam = $WM_KEYDOWN Or $wParam = $WM_SYSKEYDOWN Then ;按下
ConsoleWrite($testkey4 & " was down." & @CRLF)
Send('{RIGHT down}')
Return 1
ElseIf $wParam = $WM_KEYUP Or $wParam = $WM_SYSKEYUP Then ;弹起
ConsoleWrite($testkey4 & " was up." & @CRLF)
Send('{RIGHT up}')
EndIf
EndSelect
Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc ;==>_KeyProc
Func _CheckKeyStatus($Key)
Local $aLinkKeyList = StringSplit($LinkKeyList, '|')
Local $aKeyCode = StringSplit($KeyCode, '|')
Local $KeyArr = StringSplit($Key, ' + ', 1)
If UBound($KeyArr) > 2 Then
For $i = 1 To UBound($KeyArr) - 2
If BitAND(0x8000, _WinAPI_GetAsyncKeyState(_GetKeyAsc($KeyArr[$i], 1))) = 0 Then
Return 0 ;检测组合键是否全部按下,如果否,则返回0
EndIf
Next
EndIf
Return 1
EndFunc ;==>_CheckKeyStatus
Func _GetKeyAsc($Key, $Mode)
Local $aLinkKeyList = StringSplit($LinkKeyList, '|')
Local $aKeyCode = StringSplit($KeyCode, '|')
Switch $Mode
Case 1 ;获取单个键值
For $i = 1 To UBound($aLinkKeyList) - 1
If $aLinkKeyList[$i] = $Key Then
Return $aKeyCode[$i]
EndIf
Next
Case 2 ;获取组合键最后一个键的值
Local $KeyArr = StringSplit($Key, ' + ', 1)
For $i = 1 To UBound($aLinkKeyList) - 1
If $aLinkKeyList[$i] = $KeyArr[UBound($KeyArr) - 1] Then
Return $aKeyCode[$i]
EndIf
Next
EndSwitch
Return 0
EndFunc ;==>_GetKeyAsc
Func _exit()
_WinAPI_UnhookWindowsHookEx($hHook)
DllCallbackFree($hStub_KeyProc)
Exit
EndFunc ;==>_exit
|