#NoEnv
#SingleInstance Ignore
#ErrorStdOut
#KeyHistory 0
#Persistent
ListLines, Off
; SetBatchLines -1
Rawinput:= new Rawinput("inputback")
Rawinput.Start(),MacroOn:=true
return
f2::
if !MacroOn {
MacroOn:=!MacroOn
Rawinput.Start()
}else{
MacroOn:=!MacroOn
Rawinput.Stop()
ToolTip
}
return
inputback(Type,DeviceId,a,b,c,d){
if (Type = 0){ ;鼠标
ToolTip % "鼠标: " DeviceId "`nX: " a "`nY: " b "`n按键状态: " c "`n设备信息: " d
}
else if (Type = 1){ ;键盘
sc:=% "sc" Format("{:x}", a)
ToolTip % "键盘: " DeviceId "`nSC码: " a "`nVK码: " b "`n按键: " GetKeyName(sc) "`n按键状态: " c "`n设备信息: " d
}
}
Class Rawinput {
__New(callback){
this.DeviceMovedFn := this.DeviceMoved.Bind(this)
this.Callback := callback
}
Start(){
this.RegisterRawInputDevices(2,0x00000100) ;注册鼠标消息
this.RegisterRawInputDevices(6,0x00000100) ;注册键盘消息
OnMessage(0x00FF, this.DeviceMovedFn)
}
Stop(){
OnMessage(0x00FF, this.DeviceMovedFn, 0)
this.RegisterRawInputDevices(2,0x00000001) ;停止鼠标消息
this.RegisterRawInputDevices(6,0x00000001) ;停止键盘消息
}
RegisterRawInputDevices(usage,Flags) {
static DevSize := 8 + A_PtrSize
VarSetCapacity(RAWINPUTDEVICE, DevSize)
NumPut(1, RAWINPUTDEVICE, 0, "UShort")
NumPut(usage, RAWINPUTDEVICE, 2, "UShort") ;注册指定设备
NumPut(Flags, RAWINPUTDEVICE, 4, "Uint")
NumPut(A_ScriptHwnd, RAWINPUTDEVICE, 8, "UPtr") ;脚本的隐藏主窗口
DllCall("RegisterRawInputDevices", "Ptr", &RAWINPUTDEVICE, "UInt", 1, "UInt", DevSize )
}
Delete(){
this.Stop()
this.DeviceMovedFn := ""
}
DeviceMoved(wParam, lParam){
Critical
;获取数据长度
DllCall("GetRawInputData", "UInt", lParam, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + (A_PtrSize * 2))
VarSetCapacity(uRawInput, iSize)
;获取原始输入数据
DllCall("GetRawInputData", "UInt", lParam, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + (A_PtrSize * 2))
Type := NumGet(uRawInput, 0, "UInt") ;设备类型
DeviceId := NumGet(uRawInput, 8, "UPtr") ;设备id
DllCall("GetRawInputDeviceInfo", "Ptr",DeviceId, "UInt",0x20000007, "Ptr",0, "UIntP",size)
VarSetCapacity(info, size)
;获取设备详细信息
DllCall("GetRawInputDeviceInfo", "Ptr",DeviceId, "UInt",0x20000007, "Ptr",&info, "UIntP",size)
if (Type = 0) ;鼠标
{
x := 0, y := 0 ; 确保始终报告轴的数字
x := NumGet(uRawInput, (20+A_PtrSize*2), "Int")
y := NumGet(uRawInput, (24+A_PtrSize*2), "Int")
Button := NumGet(uRawInput, (12+A_PtrSize*2), "Int")
this.Callback.(Type,DeviceId, x, y,Button,StrGet(&info))
}
else if (Type = 1){ ;键盘
ScanCode := NumGet(uRawInput, (8+A_PtrSize*2), "UShort")
VKey := NumGet(uRawInput, (14+A_PtrSize*2), "UShort")
Message := NumGet(uRawInput, (16+A_PtrSize*2))
this.Callback.(Type,DeviceId, ScanCode, VKey,Message,StrGet(&info))
}
}
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
感谢分享
好好学习,天天向上