Hotkey/MouseClick timing issue?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
fade2gray
Posts: 85
Joined: 21 Apr 2015, 12:28

Hotkey/MouseClick timing issue?

22 Jul 2023, 09:02

Scenario:
  • Several onscreen positions have the same Y coordinate, the X coordinates are separated at 35px intervals, e.g. a row of ten 35px/35px squares.
  • Clicking on a relevant XY position will send a sequence of keystrokes.
The code I have works fine as long as I click on the coords manually, but if I try using a hotkey to invoke the clicks, things go pear-shaped.
The hotkey is simply designed to save the current mouse coords, click the relevant screen position and return the mouse to its previous position.

Code: Select all

#Numpad1::
    MouseGetPos, MX, MY
    Click 700 700 ; the centre of fith square in the row
    MouseMove, MX, MY
Return

The issue is, the hot key works, but for some reason the click's X coord is using that of the original mouse position's MX coord, i.e. if the mouse is originally at the coords 655/500, when the hotkey is used, although it appears to click in the correct position, the resulting sequence of keys sent indicates that it actually clicked at 665/700 (the centre of the fourth square) instead of 700/700.

I've tried using MouseMove, 700, 700 with a delay before the click, but no matter what else I try nothing works except to manually click.

Note: I use an algorithm that divides the screen into a grid of 35x35 cells - clicking within the bounds of a cell will return the central coordinates for that cell.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Hotkey/MouseClick timing issue?

22 Jul 2023, 09:17

1. By default, coordinates are relative to the active window.

2. Clicking can change the active window.

3. CoordMode can be used to change the point of reference.
User avatar
fade2gray
Posts: 85
Joined: 21 Apr 2015, 12:28

Re: Hotkey/MouseClick timing issue?

22 Jul 2023, 10:41

Thanks Mikey, but I don't think it has anything to do with the CoordMode. If I position the mouse over the cell I want to click prior to using the hotkey, it sends the correct key sequence. If I position the mouse several cells above the one I want to be clicked, again, it will send the correct sequence. But, if I position the mouse several cells above and either one or more cells to the left or right, using the hotkey will appear to move the mouse to the correct cell, but the subsequent key sequence sent will be that for the cell however many cells left or right of the intended target - so, I believe it's some kind of timing issue between getting the original mouse coords and sending the simulated mouse click, but no matter what combinations of commands and delays I use, it just won't work as intended. ¯\_(ツ)_/¯
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Hotkey/MouseClick timing issue?

22 Jul 2023, 11:19

The click occurs at the position that you have specified, using whatever CoordMode is in effect at that time.

This script sends nothing. A Send command would send to the keyboard cursor's position in the active window, if there is such a position. The keyboard cursor and the mouse cursor may be in different locations.
User avatar
fade2gray
Posts: 85
Joined: 21 Apr 2015, 12:28

Re: Hotkey/MouseClick timing issue?

22 Jul 2023, 12:05

Well, I don't understand what's going on. I am using CoordMode, Mouse, Window, and as I said previously, If I use the mouse to manually click on the desired cell (at X700,Y700), the correct action is performed. If I use the hotkey, Click visually moves the mouse to the correct cell and clicks it, but the action performed is that for the cell X+/-35 pixels left or right of X700 - the action performed is as if Click, %mx%, 700 was executed.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Hotkey/MouseClick timing issue?

22 Jul 2023, 12:44

I'm not able to debug a script that you have not posted. Others might!
User avatar
fade2gray
Posts: 85
Joined: 21 Apr 2015, 12:28

Re: Hotkey/MouseClick timing issue?

27 Aug 2023, 11:51

Just to wrap this up, it did turn out to be some kind of timing issue, not so much with the hotkey but some kind of lag with the rest of the associated code - I had to abandon it in my use case.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], peter_ahk and 337 guests