move cursor whenever xpos is bigger than 1000

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tatagi
Posts: 181
Joined: 23 Aug 2018, 11:17

move cursor whenever xpos is bigger than 1000

26 Sep 2020, 08:14

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..
Last edited by tatagi on 26 Sep 2020, 08:31, edited 1 time in total.
w0z
Posts: 230
Joined: 19 Jun 2014, 08:21

Re: move cursor whenever xpos is bigger than 1000

26 Sep 2020, 08:29

Code: Select all

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

If I was helpful consider Donate me. :beer: , plz :D
tatagi
Posts: 181
Joined: 23 Aug 2018, 11:17

Re: move cursor whenever xpos is bigger than 1000

26 Sep 2020, 08:35

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
w0z
Posts: 230
Joined: 19 Jun 2014, 08:21

Re: move cursor whenever xpos is bigger than 1000

26 Sep 2020, 08:51

Code: Select all

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

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

Last edited by w0z on 26 Sep 2020, 09:28, edited 2 times in total.
If I was helpful consider Donate me. :beer: , plz :D
tatagi
Posts: 181
Joined: 23 Aug 2018, 11:17

Re: move cursor whenever xpos is bigger than 1000

26 Sep 2020, 08:55

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?
w0z
Posts: 230
Joined: 19 Jun 2014, 08:21

Re: move cursor whenever xpos is bigger than 1000

26 Sep 2020, 09:05

If your PC is old and not much memory it could get stressed
If I was helpful consider Donate me. :beer: , plz :D
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: move cursor whenever xpos is bigger than 1000

26 Sep 2020, 19:20

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
my scripts
tatagi
Posts: 181
Joined: 23 Aug 2018, 11:17

Re: move cursor whenever xpos is bigger than 1000

26 Sep 2020, 22:34

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!
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: move cursor whenever xpos is bigger than 1000

28 Sep 2020, 16:25

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
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 179 guests