Problems with GetMousePos and MoveMouse

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
turbine2
Posts: 6
Joined: 15 Jun 2021, 05:10

Problems with GetMousePos and MoveMouse

15 Jun 2021, 05:30

Hi everyone,
O/S is Windows 10 and AutoHotKey version is 1.1.33.08
I have an odd problem I'm hoping that you all can help me with. I'm probably missing something obvious so extra eyes and experience are always welcome.
I have a simple script, it runs a loop and every so often moves the mouse. It's supposed to move the mouse 1 in each direction (unless at the limits of the screen) but it appears to be moving it randomly. I let it run and move 4 times and these are the results I get.

Code: Select all

Run            Start       Move1       Move2      Move3      Move4       Move 5
Screen         8486,96     11518,194   11518,388  11518,774  11518,1550  11518,2158
Window         7854,82     100886,180  10886,374  10886,760  10886,1536  10886,2144
Client         7841,-15    10873,83    10873,277  10873,663  10873,1439  1083,2047
I've tried using CoordMode, Mouse, Screen just in case that's the problem but that hasn't helped. The odd thing is that it sometimes works properly (as it it moves the mouse just 1) but I can't work out the reasoning between when it does and doesn't work.

Any hint as to where I'm going wrong would be appreciated.

Many thanks,

David

Code: Select all

^m::
   intDirection=0
   loop 
     {
     CoordMode, Mouse, Screen ;Relative, Client, Window
     MouseGetPos CurX, CurY
     if intDirection=0
       {
       if CurX>=1
          {
          MouseMove ,CurX-1, CurY
          }

       }
     if intDirection=1
       {
       if CurX<=1000
          {
          MouseMove ,CurX+1, CurY
          }
       }
     if intDirection=2
       {
       if CurY>=1
          {
          MouseMove ,CurX, CurY-1
          }
       }
     if intDirection=3
       {
       if CurY<=800
          {
          MouseMove ,CurX, CurY+1
          }
       }
     intDirection:=intDirection+1
     if intDirection=4
       {
       intDirection=0
       }
     Sleep (1 * 60 * 1000)
     Send, {Ctrl}
     Send, {Ctrl}
     }
User avatar
boiler
Posts: 16918
Joined: 21 Dec 2014, 02:44

Re: Problems with GetMousePos and MoveMouse

15 Jun 2021, 06:13

Is that exactly the script as run? The MouseMove commands didn’t include the R (relative) option? You wouldn’t even need the current position if you just moved by 1 and used the relative option, so I’m guessing you had something like that at one point, then you tried switching to absolute mode, but you left in the R, then removed them before you posted the script. Perhaps you thought you removed them when you ran it but forgot to save the changes to your script first.
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Problems with GetMousePos and MoveMouse

15 Jun 2021, 06:20

The sleep is quite long. If you move the mouse during that minute, you will have a different position at the next iteration. My test is below, also incorporating boiler's note about "R".

Code: Select all

Gosub, ^m
^m::
CoordMode, Mouse
intDirection := 0, pos := ""
Loop, 4
{
 MouseGetPos, CurX, CurY
 pos .= "`n" CurX "," CurY
 If (intDirection = 0 && CurX >= 1)
  MouseMove, -1, 0,, R
 If (intDirection = 1 && CurX <= 1000)
  MouseMove, 1, 0,, R
 If (intDirection = 2 && CurY >= 1)
  MouseMove, 0, -1,, R
 If (intDirection = 3 && CurY <= 800)
  MouseMove, 0, 1,, R
 intDirection++
 If (intDirection = 4)
  intDirection := 0
}
MsgBox, 64, Results, % SubStr(pos, 2)
Return
User avatar
boiler
Posts: 16918
Joined: 21 Dec 2014, 02:44

Re: Problems with GetMousePos and MoveMouse

15 Jun 2021, 06:30

Looking at the actual differences between iterations reported, they appear mainly to be multiples of the prior screen position (except for the one typo with the extra 0 and when it runs out of screen room) then a couple extra pixels due to the addition/subtraction of a pixel by the script. It is very repeatable, too.

That is explained by using the relative option when moving based on current position values, and it would be almost impossible to achieve if it were the result of the user moving the mouse between iterations.
Last edited by boiler on 15 Jun 2021, 06:34, edited 1 time in total.
User avatar
boiler
Posts: 16918
Joined: 21 Dec 2014, 02:44

Re: Problems with GetMousePos and MoveMouse

15 Jun 2021, 06:36

Given that, I believe if the script as posted were run (making sure to save as shown first so an old version isn’t executed), it would produce the desired results and not what was listed in the table.
turbine2
Posts: 6
Joined: 15 Jun 2021, 05:10

Re: Problems with GetMousePos and MoveMouse

17 Jun 2021, 10:38

Thanks everyone for the responses. That script is exactly as run. I have been trying to mess around with CoordMode (which is why you can see the comment with the other values) but the script there is what I used when I got those results, but as I said before, sometimes it works, sometimes it doesn't.
I'll try mikeyww's version and see if that causes the same issues.
Really appreciate the feedback, thank you.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Problems with GetMousePos and MoveMouse

17 Jun 2021, 14:53

This may or may not be related to your issue.
turbine2 wrote:
15 Jun 2021, 05:30

Code: Select all

Run            Start       
Screen         8486,96
Window         7854,82 
Client         7841,-15
Your starting values look very high. If they are the actual position that your cursor was at when you used MouseGetPos at the begining then you may need to look into using this instead of MouseMove.
MouseMove Remarks
The following is an alternate way to move the mouse cursor that may work better in certain multi-monitor configurations:

DllCall("SetCursorPos", "int", 100, "int", 400) ; The first number is the X-coordinate and the second is the Y (relative to the screen).
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Problems with GetMousePos and MoveMouse

17 Jun 2021, 17:10

Hi turbine2,

Are you running with DPI scaling other than 100%? If so, that may be the problem and this article may be helpful:

Keyboard shortcuts (hotkeys) to move mouse in multi-monitor configuration - AutoHotkey Script

The "per-monitor DPI awareness" code from lexikos was crucial in getting the mouse to move to the correct position in a multi-monitor configuration in which the monitors have different (non-100%) DPI scaling. You can see that in my Comments thread with Richard Chu under the article (click the VIEW MORE button to see all the Comments, where the troubleshooting eventually brought to light the DPI scaling issue). Regards, Joe
turbine2
Posts: 6
Joined: 15 Jun 2021, 05:10

Re: Problems with GetMousePos and MoveMouse

23 Jun 2021, 08:55

JoeWinograd wrote:
17 Jun 2021, 17:10
Hi turbine2,

Are you running with DPI scaling other than 100%? If so, that may be the problem and this article may be helpful:
Bingo, yes I am. I have two monitors; a laptop at 3840 x 2160 and 200% scaling and an external monitor 1920x1080 at 100% scaling. I will take a look at the linked article.

Many thanks,

David
turbine2
Posts: 6
Joined: 15 Jun 2021, 05:10

Re: Problems with GetMousePos and MoveMouse

23 Jun 2021, 08:56

Hellbent wrote:
17 Jun 2021, 14:53

Your starting values look very high. If they are the actual position that your cursor was at when you used MouseGetPos at the begining then you may need to look into using this instead of MouseMove.
Thank you for looking at this. Those were the values reported by WindowSpy. I'll take a look at the article.

Many thanks,

David
turbine2
Posts: 6
Joined: 15 Jun 2021, 05:10

Re: Problems with GetMousePos and MoveMouse

23 Jun 2021, 09:06

mikeyww wrote:
15 Jun 2021, 06:20
The sleep is quite long. If you move the mouse during that minute, you will have a different position at the next iteration. My test is below, also incorporating boiler's note about "R".
Thanks for that code. I've tried it but I'm not sure I have it working. If I run in and hit CTRL-M I don't see the mouse moving at all (none of the values in Window Spy change). I did change it a little and got it working (I think), it's not failed me so far anyway.

Code: Select all

^m::
CoordMode, Mouse
intDirection := 0    ; pos := ""
Loop
{
 MouseGetPos, CurX, CurY
; pos .= "`n" CurX "," CurY
 If (intDirection = 0 && CurX >= 1)
  MouseMove, -1, 0,, R
 If (intDirection = 1 && CurX <= 3700)
  MouseMove, 1, 0,, R
 If (intDirection = 2 && CurY >= 1)
  MouseMove, 0, -1,, R
 If (intDirection = 3 && CurY <= 2100)
  MouseMove, 0, 1,, R
 intDirection++
 If (intDirection = 4)
  intDirection := 0
}
;MsgBox, 64, Results, % SubStr(pos, 2)
Return
The delay in my original is fine, it's mostly just a noddy script to move the mouse a little bit every so often if I don't so that if I'm reading a page slowly the screensaver does not kick in.
turbine2
Posts: 6
Joined: 15 Jun 2021, 05:10

Re: Problems with GetMousePos and MoveMouse

25 Jun 2021, 02:14

Just wanted to come back to you all and let you know that the script I have now appears to be working. Thank you all for your help and pointers.

David

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CrowexBR, Google [Bot], Joey5, Nerafius, Rohwedder and 120 guests