Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

AutoHotkey: MouseClick coordinates

Hello experts,

I was wondering how to properly have the coordinates of MouseClick to set up in AutoHotkey.
Screenshot tools display the coordinates when I take screenshot but those coordinates are not the one to set up.
I was thinking to set up something like this:
Reference site:
https://www.autohotkey.com/boards/viewtopic.php?t=33242#p154525
+^F12::
	ToolTip, click somewhere
	KeyWait, LButton, D
	MouseGetPos, X1, Y1
	KeyWait, LButton, U
	ToolTip,  click somewhere else
	KeyWait, LButton, D
	MouseGetPos, X2, Y2
	KeyWait, LButton, U
	ToolTip
	MsgBox, X1: %X1% Y1: %Y1% X2: %X2% Y2: %Y2%
Return

Open in new window

However I don't know if there is a potential risk to add this to my AutoHotkey file as regards the options used such as:
KeyWait and Tool Tip.
If you have advice on this please let me know.
Thank you for your help.
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

No risk. They won't affect the rest of your AutoHotkey script because (1) there's both a KeyWait Down and KeyWait Up, so when you do a mouse click, you'll run through both of those chunks of code and (2) there's a ToolTip command at the end without a Text parameter, which means it will stop being displayed. You may learn more about these commands here:

https://www.autohotkey.com/docs/commands/KeyWait.htm
https://www.autohotkey.com/docs/commands/ToolTip.htm

Regards, Joe
Avatar of Luis Diaz

ASKER

Noted, I take the opportunity about this question to sort a complementary doubt.
I would like to send coordinates after the msgbox the idea is the following:
+^F12::
ToolTip, click somewhere
KeyWait, LButton, D
MouseGetPos, X1, Y1
KeyWait, LButton, U
ToolTip,  click somewhere else
KeyWait, LButton, D
MouseGetPos, X2, Y2
KeyWait, LButton, U
ToolTip
MsgBox, X1: %X1% Y1: %Y1% X2: %X2% Y2: %Y2%
MsgBox, 4,, Would you like to to send coordinates? (press Yes or No)
IfMsgBox Yes
SendRaw, X1: %X1% Y1: %Y1% X2: %X2% Y2: %Y2%
else
MsgBox You pressed No.
Return

Open in new window


However prior to SendRaw if user Click on Yes to send coordinates, I was wondering how to add the following:
Place the mouse cursor in which you want to Send coordinates.
> Place the mouse cursor in which you want to Send coordinates.

Put this in your AutoHotkey script if you want to move to the X1/Y1 coordinates:

MouseMove,%X1%,%Y1%

Or this if you want to move to the X2/Y2 coordinates:

MouseMove,%X2%,%Y2%

You may learn about this at the doc:
https://www.autohotkey.com/docs/commands/MouseMove.htm

Note that CoordMode is very important. You may learn about it at the doc:
https://www.autohotkey.com/docs/commands/CoordMode.htm

Btw, you don't need SendRaw because the X/Y variables contain coordinates that are simply integers...nothing that would be interpreted as modifiers. Regards, Joe
Thank you Joe for your comment.
I was thinking to select a saved zone in which I want to send the coordinate instead of sending coordinates related to previous click
What I have in mind is the the following:
+^F12::
ToolTip, click somewhere
KeyWait, LButton, D
MouseGetPos, X1, Y1
KeyWait, LButton, U
ToolTip,  click somewhere else
KeyWait, LButton, D
MouseGetPos, X2, Y2
KeyWait, LButton, U
ToolTip
MsgBox, X1: %X1% Y1: %Y1% X2: %X2% Y2: %Y2%
MsgBox, 4,, Would you like to to send coordinates? (press Yes or No)
IfMsgBox Yes
{
ToolTip, click somewhere
KeyWait, LButton, D
MouseGetPos, X3, Y3	
MouseMove,%X3%,%Y3%
Send, X1: %X1% `n
Sleep, 200
Send, Y1: %Y1% `n
Send, X2: %X2% `n
Sleep, 200
Send, Y2: %Y2% `n
ToolTip
}
else {
MsgBox You pressed No.
}
Return

Open in new window

After clicking on two positions I displayed the coordinates but I continue with a Msgbox in order to send coordinates.
If I click on Yes, I let the opportunity to select a saved zone or in other words to put the cursor in a saved position in which I want to send coordinates.
I tested and it works. But I don't know if I should review the structure prior to adding to my single AutoHotkey file.
Let me know what do you think in terms of AutoHotkey structure or if I should proceed in a different way.
Thank you for your help.
SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for this comment Joe.
Last questions about this topic.
Is there a way to bring to front the Msgbox because when I click on a button the MsgBox is in backward as a result I need to Go to Desktop to access to it?
I will also put and a Sleep, 100  prior sending the coordinates.
Sorry again to recall on this topic but I realized that send text is not the right strategy. I think that the best will be to send to clipboard.

I prefer send to clipboard
+^F12::
ToolTip, click somewhere
KeyWait, LButton, D
MouseGetPos, X1, Y1
KeyWait, LButton, U
ToolTip,  click somewhere else
KeyWait, LButton, D
MouseGetPos, X2, Y2
KeyWait, LButton, U
ToolTip
MsgBox, X1: %X1% Y1: %Y1% X2: %X2% Y2: %Y2%
MsgBox, 4,, Would you like to to send coordinates to the clipboard? (press Yes or No)
IfMsgBox Yes
{
  Coordinates:=X1 %X1% Y1 %Y1% X2 %X2% Y2 %Y2%
  Clipboard:=%Coordinates%
}
Else
{
  MsgBox You pressed No.
}
Return

Open in new window

I tried to put the following at coordinates but it doesn't work:
X1: %X1% Y1: %Y1% X2: %X2% Y2: %Y2%
But having some issues with ":"
Let me know what do you think.
For the record I modified my previous message.
> Is there a way to bring to front the Msgbox

Yes. It is explained very clearly in the documentation:
https://www.autohotkey.com/docs/commands/MsgBox.htm

Notice this there:

System Modal (always on top) 4096

Look at the AutoHotkey code that I've written for you and in my EE articles and videos with AutoHotkey code. You'll see values in the MsgBox command like 4096, 4100, 4112, 4132, 4144, 4160, etc. That's because the 4096 component of those numbers will keep the MsgBox on top, which is typically a good idea (as you've discovered).

So, your first MsgBox, which has Info in it, should be:

MsgBox,4160,Coordinates,X1: %X1% Y1: %Y1% X2: %X2% Y2: %Y2%

Your second MsgBox, which has a Yes/No Question in it, should be:

MsgBox,4132,Send Coordinates?,Would you like to to send coordinates?

Your third MsgBox, which has Info in it, should be:

MsgBox,4160,Coordinates not sent,You pressed No as a result your Clipboard doesn't have coordinates information.

I put a title bar in each and removed the "press Yes or No" in the second one, since it is obvious.

Regards, Joe
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok noted, thank you very much for your help.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial