Page 1 of 1

move cursor whenever xpos is bigger than 1000

Posted: 26 Sep 2020, 08:14
by tatagi
I want to make script that moves cursor to xpos 900 whenever current cursor's xpos value is bigger than 1000 (by window spy) only when notepad is running.

I tried

Code: Select all

mousegetpos, xpos, ypos
if (xpos > 1000)
{
mousemove, 900, ypos, 0
}
return


but it doesn't stay running. it just checks and closes itself..

Re: move cursor whenever xpos is bigger than 1000

Posted: 26 Sep 2020, 08:29
by w0z

Code: Select all

mousegetpos, xpos, ypos
if (xpos > 1000)
{
mousemove, 900, %ypos%, 0
}


Re: move cursor whenever xpos is bigger than 1000

Posted: 26 Sep 2020, 08:35
by tatagi
w0z wrote:
26 Sep 2020, 08:29

Code: Select all

mousegetpos, xpos, ypos
if (xpos > 1000)
{
mousemove, 900, %ypos%, 0
}

no change. I mean I want to keep the script running , not just once

Re: move cursor whenever xpos is bigger than 1000

Posted: 26 Sep 2020, 08:51
by w0z

Code: Select all

Loop
{
	mousegetpos, xpos, ypos
	if (xpos > 1000)
	{
		mousemove, 900, %ypos%, 0
	}
}

Esc::ExitApp ; stop the loop by exit the script


Re: move cursor whenever xpos is bigger than 1000

Posted: 26 Sep 2020, 08:55
by tatagi
w0z wrote:
26 Sep 2020, 08:51

Code: Select all

Loop
{
	mousegetpos, xpos, ypos
	if (xpos > 1000)
	{
		mousemove, 900, %ypos%, 0
	}
}

Esc::Reload ; stop the loop by reloading the script


thank you. it works, but this would give CPU so much stress since it keeps checking the coordinates right?

Re: move cursor whenever xpos is bigger than 1000

Posted: 26 Sep 2020, 09:05
by w0z
If your PC is old and not much memory it could get stressed

Re: move cursor whenever xpos is bigger than 1000

Posted: 26 Sep 2020, 19:20
by A_AhkUser
Alternate method, by implementing a winevent hook:

Code: Select all

myFunction() {
	MouseGetPos, mx, my
	if (mx > 1000)
		MouseMove, 900, % my
}

winTitle := "ahk_class Notepad" ; https://www.autohotkey.com/docs/misc/WinTitle.htm
coordModeMouse := "Screen" ;https://www.autohotkey.com/docs/commands/CoordMode.htm

; <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

CoordMode, Mouse, % coordModeMouse
WinWait % winTitle
EVENT_OBJECT_LOCATIONCHANGE := 0x800B
WinGet, PID, PID
hWinEventHook := DllCall("SetWinEventHook", "UInt", EVENT_OBJECT_LOCATIONCHANGE
									, "UInt", EVENT_OBJECT_LOCATIONCHANGE
									, "Ptr", 0
									, "Ptr", RegisterCallback("WinEventProc")
									, "UInt", PID
									, "UInt", 0
									, "UInt", 0) ; https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwineventhook
#Persistent
OnExit, handleExit
return

handleExit:
	if (hWinEventHook)
		DllCall("UnhookWinEvent", "Ptr", hWinEventHook) ; https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-unhookwinevent
ExitApp

WinEventProc(hWinEventHook, event, hwnd, idObject, idChild, idEventThread, dwmsEventTime) {
; https://docs.microsoft.com/en-us/windows/win32/api/winuser/nc-winuser-wineventproc
	static OBJID_CURSOR := 0xFFFFFFF7
	; https://docs.microsoft.com/en-us/windows/win32/winauto/object-identifiers
	; https://www.autohotkey.com/boards/viewtopic.php?t=32039&p=167451#p264043
	static bufferInfluxThreshold := 10
	static f := Func("myFunction")
	Critical
	if (idObject <> OBJID_CURSOR)
		return
	SetTimer % f, % -bufferInfluxThreshold
}
A_AhkUser

Re: move cursor whenever xpos is bigger than 1000

Posted: 26 Sep 2020, 22:34
by tatagi
hello , A_AhkUser

would you please tell me what the difference is from w0z's and why it looks a bit complicated?

btw thank you for your alternative, I would test it out soon!

Re: move cursor whenever xpos is bigger than 1000

Posted: 28 Sep 2020, 16:25
by A_AhkUser
tatagi wrote:
26 Sep 2020, 22:34
would you please tell me what the difference is from w0z's and why it looks a bit complicated?

Although in its principle highly correct, w0z's sample code suffers from an obvious lack: it blindly loops, indiscriminately and continuously. As for a WinEventHook, it allows you to intercept application events (here the EVENT_OBJECT_LOCATIONCHANGE one, generated when 'An object has changed location, shape, or size.') and implement a custom handling of them before giving control back to the application. Moreover, the hooking is here design in such way that if receives events only from the process to which belong the application (in the example: notepad) and really handle the event if the object associated with the event is the mouse pointer (OBJID_CURSOR). In other words, it actually does ahk checking and processing only when the cursor moves (and while hovering notepad).

Btw, one other option, taking the most of built-in common ahk commands is probably to use in synergy the three following commmands: timers, winwait and winwaitnotactive.

It looks more complicated because ahk does not provide a built-in command to set a wineventhook. On the contrary, it provides handy and easy-to-use commands to retrieve the current position of the mouse cursor and move it (i.e. MouseGetPos and MouseMove) - but there's no implication that in the background these latter commands do not unfolds against, implies as much coding efforts.


A_AhkUser