How to bring a mouse pointer to the center using mouse.move()

Hello everyone,

How to do I bring mouse pointer to the center using mouse.move(). I am confused how to do that can anyone help me please.

This seems like a pretty simple question but I am not really sure can anyone answer it for me.

My first question is, center of what?

Center of the screen. I am using a arduino leonardo just saying.

You have to define your screen size just like in the original docs.
https://www.arduino.cc/reference/en/language/functions/usb/mouse/mousemove

See minima and maxima arrays for x and y values.
So, your Arduino can't read your OS Settings, you know...

Afterwards, you should:

  1. Calculate the middle of the screen (Read value from array then /2)
  2. Move your mouse a lot to top left or write / find a tool which sends mouse position to arduino via serial
  3. Calculate desired position x / y - current position x / y
  4. Move.mouse()

So do I use a joystick module for this dr-o. And when I hook it up and run it. And move joystick does it result th mouse coordinates.

What? joystick?

Would you mind clicking on the link I posted before? All you have to do is click on the link and copy paste the parts you require.

Sorry I wasn't clear enough I actually want to use a push button to move the cursor to the center of the screen and I need to know the coordinates of mouse cursor or know how to figure it out.

Hello everyone,

Sorry I wasn’t clear enough I think in my last post so made another one trying to explain more clearly.

So I want to use a push button to move the cursor to the center of the screen and I need to know the coordinates of mouse cursor or know how to figure it out.

So can anyone help me???Please

Posts merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky “How to use this forum - please read.” post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Sorry I didn't know, I am new to the arduino forum. Thank you for letting me know, how do you take down a post.

how do you take down a post.

Let's leave both of them in this thread as a lesson to others

You could try something link this:

// Move mouse cursor to center of screen
// With Arduino Leonardo / Micro
//
// Background:
//   Mouse moves are relative to current mouse position.
//   One single mouse move can not be more than +/-127 pixels.
//

#include <Mouse.h>
const int myScreenWidth   = 1366;       // your screen width
const int myScreenHeight  =  768;       // your screen height
const byte buttonMouse2CenterPin =  9;  // your button pin. other button pin goes to GND
bool buttonState;
bool lastButtonState;

void setup() {
  Mouse.begin();
  pinMode(buttonMouse2CenterPin, INPUT_PULLUP);
}

void loop() {
  buttonState = digitalRead(buttonMouse2CenterPin);
  if (buttonState != lastButtonState) {       // state change detection
    delay(40);    // debounce 
    if (buttonState == LOW) {         // "center button" was pressed
      moveMouse2screenCenter();
    }
    lastButtonState = buttonState;
  }
}

void moveMouse2screenCenter() {
  int moveXtimes = (myScreenWidth / 127 / 2);
  int moveYtimes = (myScreenHeight / 127 / 2);
  signed char lastMoveX = (myScreenWidth / 2) - (moveXtimes * 127);
  signed char lastMoveY = (myScreenHeight / 2) - (moveYtimes * 127);
  moveMouse2UpperLeftCorner();
  for (int i = 0; i < moveXtimes; i++) {
    Mouse.move(127, 0, 0);
  }
  Mouse.move(lastMoveX, 0, 0);
  for (int i = 0; i < moveYtimes; i++) {
    Mouse.move(0, 127, 0);
  }
  Mouse.move(0, lastMoveY, 0);
}

void moveMouse2UpperLeftCorner() {
  int moveXtimes = (myScreenWidth / 127) + 1;
  int moveYtimes = (myScreenHeight / 127) + 1;
  for (int i = 0; i < moveXtimes; i++) {
    Mouse.move(-127, 0, 0);
  }
  for (int i = 0; i < moveYtimes; i++) {
    Mouse.move(0, -127, 0);
  }
}

Thank you, uxomm, I will try the code

I tried uxomm, but that did not work but it did move the mouse.

In general, mice do not report (or even KNOW) absolute positions. They only know about relative motion since the last update. Also, mouse coordinates do not match up exactly with pixels...

You can try looking into "absolute coordinate" modes. They seem somewhat problematic.

halomynibb:
I tried uxomm, but that did not work but it did move the mouse.

Can you please elaborate on "it did not work".

I tried this code with different operating systems (Windows, Mac, Linux) and it worked like expected on all different machines.
But what all these computers have in common: The mouse "speed" is turned down to a lower value than the "default" settings.
And if I set the "speed" to a much higher level, than the mouse will not move to the screen center but will "overshoot", it moves more to the lower right corner.
This has to do with the effect mentioned by westfw ("mouse coordinates do not match up exactly with pixels").

So you may ...

  1. Try to reduce the "mouse speed".
    Depending on the operation system you can find this setting at:
    Mouse pointer options / Pointer speed
    Try to set it to the lower third.

  2. Experiment with the values in function "moveMouse2screenCenter".

Uxomm: Considering this dudes message history, He didnt even change his screen resolution and kinda suprised, that the mouse moved but not to the middle...