Post by evilC » 12 Jun 2017, 09:12
This demo uses extends the "MultiMouse" example to demo selectively blocking input from one mouse, while letting input through from another mouse.
Yep, that's right - we thought it could not be done, but here it is:
Code: Select all
; ================= USER SCRIPT ================
#SingleInstance force
#NoEnv
#include <CLR>
OnExit, UnhookAndClose
GoSub, Hook
Gui, Add, Text, , Select Mouse:
mdw := new MouseDeltaWrapper("x+5 yp-3 w200")
mdw.SubscribeMove(Func("MoveEvent"))
mdw.SubscribeWheel(Func("WheelEvent"))
Gui, Show
return
^Esc::
UnhookAndClose:
GuiClose:
GoSub, UnHook
ExitApp
Hook:
hHookMouse := SetWindowsHookEx(WH_MOUSE_LL := 14, RegisterCallback("MouseMove", "Fast"))
return
UnHook:
UnhookWindowsHookEx(hHookMouse)
return
MoveEvent(x, y, mouseId){
Global mdw
if (mdw.SelectedMouse == 0 || mdw.SelectedMouse == mouseId){
DllCall("mouse_event",uint,1,int, x ,int, y,uint,0,int,0)
}
}
WheelEvent(value, mouseId){
ToolTip % "Wheel: " value ", ID: " mouseId
}
; ================= WRAPPER LIBRARY ================
class MouseDeltaWrapper {
SeenMice := {}
SelectedMouse := 0
MoveCallback := 0
__New(guiOptions := "", dllPath := "MouseDelta.dll"){
this.Callback := callback
Gui, +HwndHwnd
this.GuiHwnd := Hwnd
Gui, Add, DDL, % "hwndhDDL " guiOptions, Any||
this.hDDL := hDDL
fn := this._UserSelectedMouse.Bind(this)
GuiControl, +g, % this.hDDL, % fn
asm := CLR_LoadLibrary(dllPath)
md := asm.CreateInstance("MouseDelta")
md.SubscribeRelativeMove(this._MoveEvent.Bind(this))
md.SubscribeWheel(this._WheelEvent.Bind(this))
this.md := md
this._UserSelectedMouse()
}
SubscribeMove(callback){
this.MoveCallback := callback
}
SubscribeWheel(callback){
this.WheelCallback := callback
}
_UserSelectedMouse(){
GuiControlGet, mouseId, , % this.hDDL
this.SelectedMouse := mouseId == "Any" ? 0 : mouseId
if (this.MoveCallback != 0)
this.md.SubscribeRelativeMove(this._MoveEvent.Bind(this), this.SelectedMouse)
if (this.WheelCallback != 0)
this.md.SubscribeWheel(this._WheelEvent.Bind(this), this.SelectedMouse)
}
_AddMouseToDDL(mouseId){
GuiControl, , % this.hDDL, % mouseId
}
_UpdateMice(mouseId){
if (!this.SeenMice.HasKey(mouseId)){
this.SeenMice[mouseId] := 1
this._AddMouseToDDL(mouseId)
}
}
_MoveEvent(x, y, mouseId){
this._UpdateMice(mouseId)
if (this.MoveCallback != 0 && (this.SelectedMouse == 0 || this.SelectedMouse == mouseId)){
this.MoveCallback.Call(x, y, mouseId)
}
}
_WheelEvent(value, mouseId){
this._UpdateMice(mouseId)
if (this.WheelCallback != 0 && (this.SelectedMouse == 0 || this.SelectedMouse == mouseId)){
this.WheelCallback.Call(value, mouseId)
}
}
}
MouseMove(nCode, wParam, lParam)
{
Critical
SetFormat, Integer, D
If !nCode && (wParam = 0x200){
; Mouse movement - process
if (NumGet(lParam+0, 12, "int")){
; if the LLMHF_INJECTED flag is set, this is "injected" input (Came from mouse_event)
; Let this input through
Return CallNextHookEx(nCode, wParam, lParam)
} else {
; Block the input
Return 1
}
} else {
; Other mouse message - let through
Return CallNextHookEx(nCode, wParam, lParam)
}
}
SetWindowsHookEx(idHook, pfn)
{
;Return DllCall("SetWindowsHookEx", "int", idHook, "Uint", pfn, "Uint", DllCall("GetModuleHandle", "Uint", 0), "Uint", 0)
return DllCall("SetWindowsHookEx", "int", idHook, "Uint", pfn, "Uint", 0, "Uint", 0)
}
UnhookWindowsHookEx(hHook)
{
Return DllCall("UnhookWindowsHookEx", "Uint", hHook)
}
CallNextHookEx(nCode, wParam, lParam, hHook = 0)
{
Return DllCall("CallNextHookEx", "Uint", hHook, "int", nCode, "Uint", wParam, "Uint", lParam)
}
How it works:
In reality, ALL input is blocked, but MoveEvent still fires for all input and is passed the VID/PID of the mouse that moved.
In MoveEvent, if the input came from the selected mouse, it resends synthetic input using the mouse_event API call
The hook checks for the presence of the LLMHF_INJECTED flag, which is set to 1 if the input came from a mouse_event API call, and lets it through.
This demo uses extends the "MultiMouse" example to demo selectively blocking input from one mouse, while letting input through from another mouse.
Yep, that's right - we thought it could not be done, but here it is:
[code]
; ================= USER SCRIPT ================
#SingleInstance force
#NoEnv
#include <CLR>
OnExit, UnhookAndClose
GoSub, Hook
Gui, Add, Text, , Select Mouse:
mdw := new MouseDeltaWrapper("x+5 yp-3 w200")
mdw.SubscribeMove(Func("MoveEvent"))
mdw.SubscribeWheel(Func("WheelEvent"))
Gui, Show
return
^Esc::
UnhookAndClose:
GuiClose:
GoSub, UnHook
ExitApp
Hook:
hHookMouse := SetWindowsHookEx(WH_MOUSE_LL := 14, RegisterCallback("MouseMove", "Fast"))
return
UnHook:
UnhookWindowsHookEx(hHookMouse)
return
MoveEvent(x, y, mouseId){
Global mdw
if (mdw.SelectedMouse == 0 || mdw.SelectedMouse == mouseId){
DllCall("mouse_event",uint,1,int, x ,int, y,uint,0,int,0)
}
}
WheelEvent(value, mouseId){
ToolTip % "Wheel: " value ", ID: " mouseId
}
; ================= WRAPPER LIBRARY ================
class MouseDeltaWrapper {
SeenMice := {}
SelectedMouse := 0
MoveCallback := 0
__New(guiOptions := "", dllPath := "MouseDelta.dll"){
this.Callback := callback
Gui, +HwndHwnd
this.GuiHwnd := Hwnd
Gui, Add, DDL, % "hwndhDDL " guiOptions, Any||
this.hDDL := hDDL
fn := this._UserSelectedMouse.Bind(this)
GuiControl, +g, % this.hDDL, % fn
asm := CLR_LoadLibrary(dllPath)
md := asm.CreateInstance("MouseDelta")
md.SubscribeRelativeMove(this._MoveEvent.Bind(this))
md.SubscribeWheel(this._WheelEvent.Bind(this))
this.md := md
this._UserSelectedMouse()
}
SubscribeMove(callback){
this.MoveCallback := callback
}
SubscribeWheel(callback){
this.WheelCallback := callback
}
_UserSelectedMouse(){
GuiControlGet, mouseId, , % this.hDDL
this.SelectedMouse := mouseId == "Any" ? 0 : mouseId
if (this.MoveCallback != 0)
this.md.SubscribeRelativeMove(this._MoveEvent.Bind(this), this.SelectedMouse)
if (this.WheelCallback != 0)
this.md.SubscribeWheel(this._WheelEvent.Bind(this), this.SelectedMouse)
}
_AddMouseToDDL(mouseId){
GuiControl, , % this.hDDL, % mouseId
}
_UpdateMice(mouseId){
if (!this.SeenMice.HasKey(mouseId)){
this.SeenMice[mouseId] := 1
this._AddMouseToDDL(mouseId)
}
}
_MoveEvent(x, y, mouseId){
this._UpdateMice(mouseId)
if (this.MoveCallback != 0 && (this.SelectedMouse == 0 || this.SelectedMouse == mouseId)){
this.MoveCallback.Call(x, y, mouseId)
}
}
_WheelEvent(value, mouseId){
this._UpdateMice(mouseId)
if (this.WheelCallback != 0 && (this.SelectedMouse == 0 || this.SelectedMouse == mouseId)){
this.WheelCallback.Call(value, mouseId)
}
}
}
MouseMove(nCode, wParam, lParam)
{
Critical
SetFormat, Integer, D
If !nCode && (wParam = 0x200){
; Mouse movement - process
if (NumGet(lParam+0, 12, "int")){
; if the LLMHF_INJECTED flag is set, this is "injected" input (Came from mouse_event)
; Let this input through
Return CallNextHookEx(nCode, wParam, lParam)
} else {
; Block the input
Return 1
}
} else {
; Other mouse message - let through
Return CallNextHookEx(nCode, wParam, lParam)
}
}
SetWindowsHookEx(idHook, pfn)
{
;Return DllCall("SetWindowsHookEx", "int", idHook, "Uint", pfn, "Uint", DllCall("GetModuleHandle", "Uint", 0), "Uint", 0)
return DllCall("SetWindowsHookEx", "int", idHook, "Uint", pfn, "Uint", 0, "Uint", 0)
}
UnhookWindowsHookEx(hHook)
{
Return DllCall("UnhookWindowsHookEx", "Uint", hHook)
}
CallNextHookEx(nCode, wParam, lParam, hHook = 0)
{
Return DllCall("CallNextHookEx", "Uint", hHook, "int", nCode, "Uint", wParam, "Uint", lParam)
}
[/code]
How it works:
In reality, ALL input is blocked, but MoveEvent still fires for all input and is passed the VID/PID of the mouse that moved.
In MoveEvent, if the input came from the selected mouse, it resends synthetic input using the mouse_event API call
The hook checks for the presence of the LLMHF_INJECTED flag, which is set to 1 if the input came from a mouse_event API call, and lets it through.