mouseMove to an absolute position on screen

Asked by Marc Summers

Is there a command such as mouseMove that can move the mouse to an absolute position
such as mouseMove(0,0) not regarding the current position of the mouse?

I need to be able to move the cursor ie(mouse) to an exact absolute position
no matter where it is currently on the screen.

I have not been able to so far, to find such a command.

Please let me know if this is possible.

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Marc Summers
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
RaiMan (raimund-hocke) said :
#1
Revision history for this message
Marc Summers (2aircraft) said :
#2

mouseMove(PSRML)

Is there an example usage of this command syntax somewhere ?

Thanks for your help.

Revision history for this message
RaiMan (raimund-hocke) said :
#3

the docs say:
PSMRL – a pattern, a string, a match, a region or a location that evaluates to a click point.

What is the problem?

Revision history for this message
Marc Summers (2aircraft) said :
#4

So is it as simple as this:

given a screen resolution of 1920 x 1200

mouseMove(0,0)

or

mouseMove(1896,0)

or

mouseMove(21,1186)

Revision history for this message
RaiMan (raimund-hocke) said :
#5

Did you really read the docs?

what you have is the offset variant.

the usage is the same as with click and all the other mouse actions.

Revision history for this message
Marc Summers (2aircraft) said :
#6

Yes Sir, I did read the document, and did a lot of other research on this also. :)
I have used the OffSet method quite a bit in the code I have already written.

But I need a way to tell the cursor to move to an absolute position
irregardless of its current position on screen and the offset method
will not do that.

If there was just an example code that showed what the syntax is.

Thanks for your help.

Revision history for this message
Marc Summers (2aircraft) said :
#7

I was just now doing some additional research and it looks like
I might be able to use PyAutoGUI module.

Here is an example code piece:

XY coordinates have 0, 0 origin at top left corner of the screen. X increases going right, Y increases going down.

>>> pyautogui.moveTo(x, y, duration=num_seconds)

Revision history for this message
RaiMan (raimund-hocke) said :
#8

the same effort in the SikuliX docs together with my hints would have revealed:

mouseMove(Location(x, y))

Revision history for this message
Mike (maestro+++) said :
#9

Also : do you need to actually move the mouse?

If for example you want to click on something, Sikulix will move the mouse for you in order to perform the click action.

Revision history for this message
Marc Summers (2aircraft) said :
#10

Thanks for your help.
It is much appreciated.
I have much to learn about Python and Sikuli.
So far I am finding it very interesting.

Revision history for this message
Mike (maestro+++) said :
#11

Marc, you can control the mouse in different ways. For example this code moves the mouse to a location and after a while clicks on that screen position.

hover(Location(1308, 821))
wait(10)
click()

This location I got from the location button on the IDE. Equally it will work by using a region from the region button or an image from the Take screenshot button. Sikulix shows a lot of polymorphism - many functions work with a variety of types.

Revision history for this message
Mike (maestro+++) said :
#12

Apologies Marc, I just tested that code again and it doesn't work on Windows 10 now.

It's fine with a region but a location or an image give something like this:

[error] RobotDesktop: checkMousePosition: should be L[960,1059]@S(0) but after move is L[960,1060]@S(0) Possible cause in case you did not touch the mouse while script was running: Mouse actions are blocked generally or by the frontmost application. You might try to run the SikuliX stuff as admin.

[log] CLICK on L[960,540]@S(0) (686 msec)

The symptom is the cursor goes to the right spot but after hovering returns to the middle of the screen, and it looks like it clicked there.

 The documentation says " A someRegion.click() will either click the center of the given Region or the lastMatch, if any is available ". I can't obviously see why it sometimes thinks there is a lastMatch and sometimes not so you'll need to experiment if you use this kind of construct.

Revision history for this message
Mike (maestro+++) said :
#13

More apologies: I see now it works for Take Screenshot ie an image. That makes sense. For a region or location there is no find involved so no lastMatch. For an image there must be a find which will then give a lastMatch.

I can only think I got some inconsistent results because the IDE seems to occasionally hold state data between program runs. I noticed this when I was posting a program on here a few days ago. I noticed a variable was not initialised. The initialisation must have got deleted during editing. It ran alright in the IDE but when I closed it down and restarted, the IDE errored with undeclared name.

Revision history for this message
Marc Summers (2aircraft) said :
#14

Wow! Thanks Mike for all of your help.
I found that the syntax mouseMove(Location(x, y))
works OK on the Mac and should work on Win 10 too.

But I do have one other question.

I am trying to do a key press sequence using
type()

What I am trying to do is this:

On the MAC if you press SHIFT CMD and the $/4 key
you will get an XY position marker on the cursor point.

The problem is that I don't know what the KEY attribute is
for the $/4 key. I have tried DOLLAR, but that is not the correct
attribute. So do you know what the correct key attribute would
be?

type(Key.SHIFT, Key.CMD, Key.???) <<< can you answer this one please?

Thanks.

Revision history for this message
Mike (maestro+++) said :
#15

I'm strictly Windows but normally you would do something like:

 type("4", Key.SHIFT + Key.CMD)

Revision history for this message
Marc Summers (2aircraft) said :
#16

Thanks again Mike:

It works great!

Thanks for all of your help. :)