How to calculate mouse position for machines with different display settings

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jdd30143
Posts: 4
Joined: 13 Jun 2018, 14:03

How to calculate mouse position for machines with different display settings

17 Oct 2018, 08:56

I have computers with different display settings and want to be able to move the cursor for different button positions using the same script by only changing one value. The screen display settings for the OtherDisplayX and OtherDisplayY may also change at a later time so I'm trying to avoid large changes in the future. The development is being done on the laptop. The issue is I cannot seem to get it to do the calculation correctly and I must admit I'm struggling with the syntax.

To observe my problem, changing the parameter OtherDisplay between 0 and 1 below is what I had hoped to do.


!1:: ;alt+1 hotkey to run

#SingleInstance, Force ;if there is another version running, stop it and run new version

;The purpose of this script is to use the same script for computers with different
;display settings by changing 1 value. Screen coordinates are used because the window name
;is sometimes not easily discovered or provided.

; Use screen coordinantes for mouse, not window
CoordMode,Mouse,Screen

OtherDisplay=0 ;Change OtherDisplay to 1 to use OtherDisplayX and OtherDisplayY values

;Variables to modify if displat size changes at a later date
LaptopDisplayX=1366
LaptopDisplayY=768
OtherDisplayX=1600
OtherDisplayY=900

;~ The calculation being performed is:
;~ Add this line after ever XVal = nnn statement
;~ If Other (XVal = XVal * OtherDisplayX / LaptopDisplayX)
;~ Add this line after ever YVal = nnn statement
;~ If Other (YVal = YVal * OtherDisplayY / LaptopDisplayY)

;~ Below is an example of one button press, there are 25

TargetButtonX=683 ;this line is added for every X button coordinance
If(OtherDisplay) {
TargetButtonX = Round(TargetButtonX * OtherDisplayX / LaptopDisplayX)
}
MsgBox, %TargetButtonX%

TargetButtonY=384 ;this line is added for every Y button coordinance
If(OtherDisplay) {
TargetButtonY = Round(TargetButtonY * OtherDisplayY / LaptopDisplayY)
}
MsgBox, %TargetButtonY%

MouseMove, TargetButtonX, TargetButtonY

!2::ExitApp
Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to calculate mouse position for machines with different display settings

17 Oct 2018, 09:26

Hallo,
just use MouseMove(X,Y) on all machines:

Code: Select all

CoordMode,Mouse,Screen
Global LaptopDisplayX, LaptopDisplayY
LaptopDisplayX=1366
LaptopDisplayY=768
Return

!1:: ;alt+1 hotkey to run
MouseMove(683,384)
Return

MouseMove(X,Y)
{ ;MouseMove(TargetButtonX,TargetButtonY)
	MouseMove,% X*A_ScreenWidth//LaptopDisplayX
	,Y*A_ScreenHeight//LaptopDisplayY
}
or the factors Sx, Sy:

Code: Select all

CoordMode,Mouse,Screen
Global Sx, Sy
Sx := A_ScreenWidth/1366
Sy := A_ScreenHeight/768
Return

!1:: ;alt+1 hotkey to run
MouseMove,% 683*Sx, 384*Sy
Return
jdd30143
Posts: 4
Joined: 13 Jun 2018, 14:03

Re: How to calculate mouse position for machines with different display settings

17 Oct 2018, 14:37

Thanks very much, your way is better. I didn’t think to look for global screen settings.
I was spending all my time trying to decide why I couldn’t get the TargetButtonX and TargetButtonY equations to work, and still haven’t. Using the debug feature in the editor seemed to give me confusing results as instead of showing a result from the equation it shows all the individual values.
jdd30143
Posts: 4
Joined: 13 Jun 2018, 14:03

Re: How to calculate mouse position for machines with different display settings

17 Oct 2018, 16:45

I do have another question. Can you explain the MouseMove, % syntax?
Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to calculate mouse position for machines with different display settings

18 Oct 2018, 00:10

Hallo,
in https://autohotkey.com/docs/Variables.htm#Expressions

Force an expression: An expression can be used in a parameter that does not directly support it (except OutputVar parameters) by preceding the expression with a percent sign and a space or tab. …
jdd30143
Posts: 4
Joined: 13 Jun 2018, 14:03

Re: How to calculate mouse position for machines with different display settings

18 Oct 2018, 05:16

Thank you, great! ... and Fast, much appreciated.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Mannaia666 and 411 guests