Custom hotkey for mouse movement?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
paik1002
Posts: 355
Joined: 28 Nov 2015, 02:45

Custom hotkey for mouse movement?

29 Apr 2017, 06:36

AHK has built-in hotkeys for mouse buttons including the wheel(https://autohotkey.com/docs/KeyList.htm#Mouse),
but doesn't seem to have a hotkey for mouse movement.

Is there a way to make mouse movement as a custom hotkey, e.g. mouse_move command, without using SetTimer?
I would like to apply this custom hotkey is to the following sample script.

Code: Select all

#IfWinActive ahk_class notepad
mouse_move::			; mouse movement detected
^!+mouse_move::			; ctrl+alt+shift+mouse movement detected
CapsLock & mouse_move::		; capslock+mouse movement detected
{
   tooltip mouse has moved!
   return
}
#If
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Custom hotkey for mouse movement?

29 Apr 2017, 07:20

You can use a mouse hook to detect mouse movements, here is a simple example, holding shift and moving the mouse shows a tooltip,

Code: Select all

#Persistent
mh:=new mouseHook("g")
mh.hook()
g(h,x,y){
	if GetKeyState("Shift")
		ToolTip, % "Mouse moved at " x " " y " while holding Shift"
}
class mouseHook{
	; User methods
	hook(){
		if !this.hHook												
			this.hHook:=DllCall("User32.dll\SetWindowsHookEx", "Int", 14, "Uint", this.regCallBack:=RegisterCallback(this.LowLevelMouseProc,"F",4, &this), "Uint", 0, "Uint", 0, "Ptr")
		return
	}
	unHook(){
		if this.hHook
			DllCall("User32.dll\UnhookWindowsHookEx", "Uint", this.hHook)
		return this.hHook:=""
	}
	; Internal methods.
	__new(callbackFunc){
		this.callbackFunc:=callbackFunc
	}
	LowLevelMouseProc(args*) {  
		; (nCode, wParam, lParam)
		Critical
		this:=Object(A_EventInfo),nCode:=NumGet(args-A_PtrSize,"Int"), wParam:=NumGet(args+0,0,"Ptr"), lParam:=NumGet(args+0,A_PtrSize,"UPtr") 
		x:=NumGet(lParam+0,0,"Int"),y:=NumGet(lParam+0,4,"Int"),flags:=NumGet(lParam+0,12,"UInt")
		tf:=Func(this.callbackFunc).Bind(flags,x,y)
		SetTimer, % tf, -0
		return DllCall("User32.dll\CallNextHookEx","Uint",0, "Int", nCode,"Uint", wParam,"Uint",lParam)
	}
	__Delete(){  
		this.unHook()
		if this.regCallBack
			DllCall("GlobalFree", "Ptr", this.regCallBack)
		return 
	}
}
esc::exitapp
paik1002
Posts: 355
Joined: 28 Nov 2015, 02:45

Re: Custom hotkey for mouse movement?

29 Apr 2017, 07:44

Thank you for providing help. Unfortunately however, I am unfamiliar with its use/application.
Would you be kind enough to show how it could be applied to my sample script above, especially with the #IfWinactive command?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Custom hotkey for mouse movement?

29 Apr 2017, 11:49

Another example then,

Code: Select all

mouse_move_mh:=new mouseHook("mouse_move")
mouse_move_mh.hook()
capslock_mh:=new mouseHook("capslock_mouse_move")
ctrl_alt_shift_mh:=new mouseHook("ctrl_alt_shift_mouse_move")

~*Capslock up::capslock_mh.unhook() ; Turns off the hook unconditionally
~*shift up::ctrl_alt_shift_mh.unhook()

#IfWinActive, ahk_exe notepad.exe
Capslock::capslock_mh.hook()		; Turn on the hook in notepad, with capslock
^!Shift::ctrl_alt_shift_mh.hook()
#if

mouse_move(h,x,y){
	IfWinActive ahk_exe notepad.exe
			ToolTip, % "Mouse moved at " x " " y " in notepad",0,0,1
}

ctrl_alt_shift_mouse_move(h,x,y){
	ToolTip, % "Mouse moved at " x " " y " while holding ctrl+alt+shift",0,25,2
}

capslock_mouse_move(h,x,y){
	ToolTip, % "Mouse moved at " x " " y " while holding CapsLock",0,50,4
}
class mouseHook{
	; User methods
	hook(){
		if !this.hHook												
			this.hHook:=DllCall("User32.dll\SetWindowsHookEx", "Int", 14, "Uint", this.regCallBack:=RegisterCallback(this.LowLevelMouseProc,"F",4, &this), "Uint", 0, "Uint", 0, "Ptr")
		return
	}
	unHook(){
		if this.hHook
			DllCall("User32.dll\UnhookWindowsHookEx", "Uint", this.hHook)
		return this.hHook:=""
	}
	; Internal methods.
	__new(callbackFunc){
		this.callbackFunc:=callbackFunc
	}
	LowLevelMouseProc(args*) {  
		; (nCode, wParam, lParam)
		Critical
		this:=Object(A_EventInfo),nCode:=NumGet(args-A_PtrSize,"Int"), wParam:=NumGet(args+0,0,"Ptr"), lParam:=NumGet(args+0,A_PtrSize,"UPtr") 
		x:=NumGet(lParam+0,0,"Int"),y:=NumGet(lParam+0,4,"Int"),flags:=NumGet(lParam+0,12,"UInt")
		tf:=Func(this.callbackFunc).Bind(flags,x,y)
		SetTimer, % tf, -0
		return DllCall("User32.dll\CallNextHookEx","Uint",0, "Int", nCode,"Uint", wParam,"Uint",lParam)
	}
	__Delete(){  
		this.unHook()
		if this.regCallBack
			DllCall("GlobalFree", "Ptr", this.regCallBack)
		return 
	}
}
esc::exitapp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, ArkuS, Nerafius and 111 guests