本帖最后由 onestone01 于 2020-7-6 09:01 编辑
一个比较笨的方法,基本上够用。效果不理想。缺点长按组合键,会不停的执行。希望有更好的方法#include <WinAPI.au3>
#include <WindowsConstants.au3>
Global $g_hHook, $g_hStub_KeyProc, $g_sBuffer = "", $WinDOWN = 0
Example()
Func Example()
OnAutoItExitRegister("Cleanup") ;退出时移除钩子
Local $hMod
$g_hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
$hMod = _WinAPI_GetModuleHandle(0)
$g_hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($g_hStub_KeyProc), $hMod)
While 1
Sleep(10)
WEnd
EndFunc ;==>Example
; ===========================================================
; callback function
; ===========================================================
Func _KeyProc($nCode, $wParam, $lParam)
Local $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
If $nCode < 0 Then
Return _WinAPI_CallNextHookEx($g_hHook, $nCode, $wParam, $lParam)
EndIf
$wVKey = DllStructGetData($tKEYHOOKS, "vkCode")
Switch $wVKey
Case 27 ; Esc 键
Exit
Case 91, 92 ;左右 Win 键
If $wParam = $WM_KEYDOWN Then ;按下
$WinDOWN = 1
ToolTip('WIN DOWN')
ElseIf $wParam = $WM_KEYUP Then ;弹起
$WinDOWN = 0
ToolTip('WIN UP')
EndIf
Return 1
Case 9, 19, 48 To 57, 65 To 90
If $WinDOWN = 1 AND $wParam = $WM_KEYDOWN Then
Send('#' & _KEYChr($wVKey))
$keyDOWN = 1
Return 1
EndIf
EndSwitch
Return _WinAPI_CallNextHookEx($g_hHook, $nCode, $wParam, $lParam)
EndFunc ;==>_KeyProc
Func _KEYChr($Key)
Switch $Key
Case 9
Return ('{TAB}')
Case 19
Return ('{PAUSE}')
Case 48 To 90 ;48 To 57 => 0 - 9 键 |65 To 90 => a - z 键
Return StringLower(Chr($Key)) ;返回 ASCII 码 并转换为小写
EndSwitch
Return ''
EndFunc ;==>_KEYChr
Func Cleanup()
_WinAPI_UnhookWindowsHookEx($g_hHook)
DllCallbackFree($g_hStub_KeyProc)
EndFunc ;==>Cleanup
|