MousePos and MouseClick Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
iistuk
Posts: 8
Joined: 08 Nov 2019, 04:23

MousePos and MouseClick

08 Nov 2019, 04:40

Hi everyone,

im having an issue with the following code:

Code: Select all

f1::
{ 
mousegetpos, start_x, start_y

ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, D:\test.bmp
if (ErrorLevel = 2)
    MsgBox Could not conduct the search.
else if (ErrorLevel = 1)
    MsgBox Icon could not be found on the screen.
else 
   PosX:=FoundX+10
   PosY:=FoundY+10
   mouseclick, left, %PosX%, %PosY%
   mousemove, %start_x%, %start_y%
   mousegetpos, end_x, end_y
   MsgBox start pos at %start_x%x%start_y%. End pos at %end_x%x%end_y%.

 }
return
What i am trying to do is:
1) Remember the current postion of the mouse
2) Find a specific icon on the screen
3) Click 10 pixels right, and 10 pixels below the top left pixel of that icon
4) Return the mouse to the position it was before

Everything up to point #4 works as it should, however my mouse does not end on its starting position. This is why i added MsgBoxes which print the starting and end positions of the mouse.
Here is the screenshot:
https ibb.co /TT0HQTJ Broken Link for safety


I suspect it has something to do with Coord Mouse, but i wasnt able to find a solution...

Thanks in advance



EDIT:
The problem is definitely multiple monitors, when i disable everything but my main one, the script works fine. But with more than 1 monitor even the

Code: Select all

CoordMode, Mouse, Screen
f1::
 {
    MouseGetPos, PosX, PosY
    mousemove, 100, 100
    sleep 200
    MouseMove, %PosX%, %PosY%
 }
return
does not work....
aifritz
Posts: 301
Joined: 29 Jul 2018, 11:30
Location: Germany

Re: MousePos and MouseClick

08 Nov 2019, 10:19

Hi iistuk,

your curly brackets are not correct:

Code: Select all

f1::
mousegetpos, start_x, start_y
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, D:\test.bmp
if (ErrorLevel = 2)
    MsgBox Could not conduct the search.
else if (ErrorLevel = 1)
    MsgBox Icon could not be found on the screen.
else { 
   PosX:=FoundX+10
   PosY:=FoundY+10
   mouseclick, left, %PosX%, %PosY%
   mousemove, %start_x%, %start_y%
   mousegetpos, end_x, end_y
   MsgBox start pos at %start_x%x%start_y%. End pos at %end_x%x%end_y%.
 }
return
I could not test it, but you may try it also with the "CoordMode, Mouse, Relative" command.
User avatar
boiler
Posts: 16961
Joined: 21 Dec 2014, 02:44

Re: MousePos and MouseClick

08 Nov 2019, 10:34

All of those mouse commands and the ImageSearch were relative to the active window since you didn't change the CoordMode for them. The problem probably is that when you perform the MouseClick, it activates that window which changes the reference point for all of the coordinates because that's not the window that was active when you ran the script. To avoid this, change the CoordMode for both Mouse and Pixel to Screen so the reference point doesn't change when a new window becomes active. Put these lines at the top of your script:

Code: Select all

CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
(and of course @aifritz is correct about the missing braces marking the code block)
iistuk
Posts: 8
Joined: 08 Nov 2019, 04:23

Re: MousePos and MouseClick

11 Nov 2019, 01:46

Thank you both for your answers. Unfortunately the issue is still unresolved.

Will have to try something else. Thanks again for your help
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: MousePos and MouseClick

11 Nov 2019, 05:27

I have seen others post about issues when they have very large monitors with high resolution or something along those lines.
So do you have a jumbotron for a monitor?

If not, try running this as/in ITS OWN SCRIPT.

Code: Select all

#SingleInstance,Force
CoordMode,Mouse,Screen

return

*Esc::ExitApp

F1::
	MouseGetPos,sx,sy
	MouseMove,0,0
	Msgbox, At x:0 y:0
	ToolTip,Moving Again In 2 Seconds.
	Sleep,2000
	ToolTip,
	MouseMove,sx,sy
	MouseGetPos,ex,ey
	Msgbox,Back At Start Position `n`nStart X: %sx%   <----> Current X: %ex%`nStart Y: %sy%   <----> Current Y: %ey% 
	return

iistuk
Posts: 8
Joined: 08 Nov 2019, 04:23

Re: MousePos and MouseClick

12 Nov 2019, 03:07

Hellbent wrote: I have seen others post about issues when they have very large monitors with high resolution or something along those lines.
So do you have a jumbotron for a monitor?

If not, try running this as/in ITS OWN SCRIPT.

Code: Select all

#SingleInstance,Force
CoordMode,Mouse,Screen

return

*Esc::ExitApp

F1::
	MouseGetPos,sx,sy
	MouseMove,0,0
	Msgbox, At x:0 y:0
	ToolTip,Moving Again In 2 Seconds.
	Sleep,2000
	ToolTip,
	MouseMove,sx,sy
	MouseGetPos,ex,ey
	Msgbox,Back At Start Position `n`nStart X: %sx%   <----> Current X: %ex%`nStart Y: %sy%   <----> Current Y: %ey% 
	return


Hi Hellbent,

this is what i get after running your script on my second or third monitor (everything seems to be fine on my main monitor)
https://ibb.co/2KXCcgZ

For some reason your script works fine on my main, but the script that i posted dosen't work on neither of the three.

Regards,
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: MousePos and MouseClick

12 Nov 2019, 06:40

iistuk wrote:
12 Nov 2019, 03:07

https ibb.co /2KXCcgZ Broken Link for safety
I don't know how to view that.
iistuk
Posts: 8
Joined: 08 Nov 2019, 04:23

Re: MousePos and MouseClick

12 Nov 2019, 06:49

Hellbent wrote:
12 Nov 2019, 06:40
iistuk wrote:
12 Nov 2019, 03:07

https ibb.co /2KXCcgZ Broken Link for safety
I don't know how to view that.
Add :/ after https
remove space before ibb.co
remove space after ibb.co

I do not know how could i upload the link without it being separated into smaller parts.
I will try to PM you a direct link.
Thanks
gregster
Posts: 9021
Joined: 30 Sep 2013, 06:48

Re: MousePos and MouseClick

12 Nov 2019, 06:52

I fixed it for now in your post above ( https://ibb.co/2KXCcg ).
As a new user you are unfortunately limited in posting URLs... and sending PMs, afaik.

Edit:
But you should be able to upload images to the forum via the forum editor: Scroll down to Attachments > Add files > Place inline
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: MousePos and MouseClick

12 Nov 2019, 07:11

iistuk wrote:
12 Nov 2019, 03:07
For some reason your script works fine on my main, but the script that i posted dosen't work on neither of the three.
I would guess that the reason yours isn't working has to do with where you put your CoordMode
It needs to be up at the top of your script in the auto-execute section. Or at the very least inside your hotkey.

As for the second problem of it not working on your two other monitors, like I had said, I have seen others saying something similar before, but I don't remember all the details.

I have a fuzzy recollection of them needing to use the dllcall that moves the cursor to fix it, but to be honest its been awhile.

To help move forward I suggest that you post some info about your two other monitors and do a search for a dllcall that can move your cursor.
gregster
Posts: 9021
Joined: 30 Sep 2013, 06:48

Re: MousePos and MouseClick  Topic is solved

12 Nov 2019, 07:22

There is this in the MouseMove docs:
https://www.autohotkey.com/docs/commands/MouseMove.htm#Remarks wrote:The following is an alternate way to move the mouse cursor that may work better in certain multi-monitor configurations:

Code: Select all

DllCall("SetCursorPos", "int", 100, "int", 400)  ; The first number is the X-coordinate and the second is the Y (relative to the screen).
iistuk
Posts: 8
Joined: 08 Nov 2019, 04:23

Re: MousePos and MouseClick

13 Nov 2019, 01:29

The DLL thing worked.

Seems like a really friendly community here on AHK, thank you all for your suggestions.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Cmecu, joefiesta and 294 guests