Sunday, September 5, 2010

Examine Mouse Positions in AHK

A truly useful script to have running while writing Mousey Scripts.
I have mine set to z

CoordMode, Mouse, Screen
MouseGetPos, X, Y
MsgBox %X%, %Y%
return

AHK, Mouse Clicks, and Remote Desktop Connection (RDC)

I was having fits with mouse clicks in AutoHotKey scripts not clicking on the right spot on the screen. I originally "solved" this by including the all - important line

CoordMode, Mouse, Screen

at the beginning of each AHK script, which tells the script to override the default behavior (coordinates relative to active window) and uses coordinates relative to the actual monitor dimensions. This eliminates errors due to the wrong window (like WebDiagLog Boxes) stealing the focus. But here is another factor to consider:

When you RDC to a computer and run AHK scripts on the server computer, the coordinates of mouse clicks will vary based on the screen resolution settings on the client computer. The converse is true as well: if you author a script remotely on a client computer using RDC, when to go to run the script locally on the server without RDC the coordinates revert to the local monitor.

Here is how I deal with this:

CoordMode, Mouse, Screen
WinGetPos,,,currentw,curretnh,Program Manager
authorw := 1280
authorh := 800
wfactor := currentw/authorw
hfactor := currenth/authorh
x := 36
y := 170
xfinal := x*wfactor
yfinal := y*hfactor
Click %xfinal%, %yfinal%

Now any Client computer can author a script on a server, and it will run locally on the server and on any other client, no matter what the screen resolution is.

Addendum:
You can also examine the screen resolution using this javascript bookmarklet:
alert(screen.width);
alert(screen.height);