Can't get a handle on Mouse.move()

I'm just not understanding how to use Mouse.move(). I want to move the mouse pointer to 2 specific spots on the screen and I can't do it.

I understand each move is relative to its last position and that's what I just can't wrap my head around and figure out how to do that. I don't know why there isn't a specific x,y function to move the mouse to that x,y point and be done with it.

So, I need help with the mechanics of thinking about how to move the mouse to a specific point and then move to another specific point from that last point?

I looked at the mouse.move example but I'm not using buttons and I can't correlate its code to my requirements.

Thanks for any help...

int  xb = 100;
int  yb = 100;
int  x = 0;
int  y = 0;

void setup() {
  Serial.begin(9600);
  Mouse.begin();
}

void loop() {
  x = 0;
  y = 0;
  Serial.print(x);
  Serial.print("x");
  Serial.println(y);
  Mouse.move(x, y, 0);
  delay(2000);

  x = 100;
  y = 100;
  Serial.print(x);
  Serial.print("x");
  Serial.println(y);
  Mouse.move(x, y, 0);
  delay(2000);

  x = -200;
  y = -200;
  Serial.print(x);
  Serial.print("x");
  Serial.println(y);
  Mouse.move(x, y, 0);
  delay(2000);

  x = 100;
  y = 100;
  Serial.print(x);
  Serial.print("x");
  Serial.println(y);
  Mouse.move(x, y, 0);
  delay(2000);

  x = -200;
  y = -200;
  Serial.print(x);
  Serial.print("x");
  Serial.println(y);
  Mouse.move(x, y, 0);
  delay(2000);

}

What happens when you run the program ? What do you want to happen when you run the program ?

That is not a full sketch, the #include <Mouse.h> is missing.
The Mouse.move() function accepts a “signed char”, that means it can only move form -128 to +127.
https://www.arduino.cc/en/Reference/MouseMove

I don’t understand what you want to do. But I have altered your sketch to make the mouse cursor make square boxes that increase in size. I do that just once in the setup() function, so I can use my mouse after that.

#include <Mouse.h>

int  xb = 100;
int  yb = 100;
int  x = 0;
int  y = 0;

void setup() {
  Serial.begin(9600);
  Mouse.begin();

  while(!Serial);    // Wait until serial monitor has started.

  for( int i=1; i<=4; i++) {
    Serial.print("x,y = ");

    x = 0;
    y = 0;
    Serial.print(x);
    Serial.print(",");
    Serial.print(y);
    Mouse.move(x, y, 0);
    delay(500);

    Serial.print("    ");
  
    x = 25 * i;
    y = 0;
    Serial.print(x);
    Serial.print(",");
    Serial.print(y);
    Mouse.move(x, y, 0);
    delay(500);
  
    Serial.print("    ");
  
    x = 0;
    y = 25 * i;
    Serial.print(x);
    Serial.print(",");
    Serial.print(y);
    Mouse.move(x, y, 0);
    delay(500);
  
    Serial.print("    ");
  
    x = -25 * i;
    y = 0;
    Serial.print(x);
    Serial.print(",");
    Serial.print(y);
    Mouse.move(x, y, 0);
    delay(500);
  
    Serial.print("    ");
  
    x = 0;
    y = -25 * i;
    Serial.print(x);
    Serial.print(",");
    Serial.print(y);
    Mouse.move(x, y, 0);
    delay(500);

    Serial.println();
  }
}


void loop() {
}

A few have altered the library to make it possible to use absolute position for the mouse:
http://forum.arduino.cc/index.php/topic,94140.0.html

I don’t know how to use that.

I'm just not understanding how to use Mouse.move(). I want to move the mouse pointer to 2 specific spots on the screen and I can't do it.

That is most likely because you are expecting Mouse.move() to make the mouse move TO a specific location. That is NOT what it does. It makes the mouse move to some new position, relative to where it currently is. That new position is determined by a number of factors, including screen size, mouse acceleration, etc. that the PC controls.

You can NOT make the mouse cursor move to specific spots.

The Mouse.move() function accepts a "signed char", that means it can only move form -128 to +127

So, if I need to move the mouse pointer further than 127 pixels away from the current position, I would have to do it using multiple moves?

So, if I need to move the mouse pointer further than 127 pixels away from the current position, I would have to do it using multiple moves?

You are still assuming, incorrectly, that you can move the mouse N pixels in any direction.

Well, maybe you can help me out with my thinking? So we have an N pixel limit of -127 to +127 in any direction, is that a correct statement?

There is a limit. The arguments are, weirdly, signed chars, so they can hold values in the range -128 to 127. The values do not correspond to pixels, though.

When the Teensy knows the screen size, then it can move to an absolute position: https://www.pjrc.com/teensy/td_mouse.html But the Teensy must know the screen size, or else it won't work.

The Arduino Leonard/Micro doesn't know the screen size, or the accelleration or any other mouse setting on the computer, and can't go to an absolute position with the normal library.

I think the reference should be changed. https://www.arduino.cc/en/Reference/MouseMove At least a sentence like: "the amount to move is not the number of pixels on the screen" should be added.

Well this seems complex and confusing (for a newbie)...why did Ardunio develop the mouse lib that way? You would think they would want to make it fairly easy to figure out and use but it seems they went in the exact opposite direction.

I'll keep looking for more tutorials and try to figure it out...the 'light bulb' hasn't lit up yet in my brain yet.

why did Ardunio develop the mouse lib that way?

The mouse library is designed to interact with the mouse driver on the PC. You need to ask why the mouse driver developers didn't implement functions to move a mouse to an exact position. I think the answer to that is fairly obvious. People moving a mouse move it until it is where it needs to be because they can see where the mouse is and where they want it.

Moving to an exact position makes little sense, since the application it is sending mouse commands to may not be full screen, may not be positioned where the Arduino thinks it is, and the screen size is not known to the Arduino.

So you're saying its the fault of the PC OS...that they don't provide an API or other method call for other folks to use to position the mouse pointer to an exact X,Y point on the screen. Fair enough.

So why didn't the Ardunio folks look at that and say, hey what the PC developers gave us is crap, but we can do better for our customers and give them an X,Y call to position the mouse pointer to an exact spot on the screen?

It obviously can be done since the Teensy folks did it and it works.

Maybe the link I should be looking at is this one: http://www.WhyDidntArdunioThinkEnoughOfTheirCustomersToWriteMouse.moveToLikeTheTeensyFolksDid.com

Moving to an exact position makes little sense.

I beg to differ...it makes a lot of sense...if I were able to move to an X,Y spot, I can take care of the pixel sync problems myself...easy to figure those out and make the X,Y work on top of the app I'm trying to mouse click on. I've already done it using a Teensy board.

Now, I'm trying to make it work on a Leonardo board and am having difficulty understanding how to make this mouse move work for my project to interact with a windows app via mouse clicks.

Using an Ardunio board, mouse move should not be this difficult, but for me it is.

So you're saying its the fault of the PC OS...that they don't provide an API or other method call for other folks to use to position the mouse pointer to an exact X,Y point on the screen. Fair enough.

They don't, because that is NOT what a mouse is meant to do.

There are other input devices, tablets, etc., for cases where exact positioning is desired. Those devices interact with a single application, at a known position and size on the screen, which is a known size.

So why didn't the Ardunio folks look at that and say, hey what the PC developers gave us is crap, but we can do better for our customers and give them an X,Y call to position the mouse pointer to an exact spot on the screen?

Because that is not possible. The Mouse library does exactly what a mouse is supposed to do. You seem to want something different, so feel free to develop it.

It obviously can be done since the Teensy folks did it and it works.

Not exactly. The Teensy is, somehow, getting the screen size. I don't know how. But, it still doesn't know anything about the mouse driver configuration. You are, of course, free to ditch the Arduino and use a Teensy.

Maybe the link I should be looking at is this one:

I certainly hope you get the help you need there.

You are, of course, free to ditch the Arduino and use a Teensy.

I'd like to use an Ardunio but with the kind of negative attitude you have, I feel very unwelcome here. You kind of give me the impression that this is a good old boys club and without 62,000+ posts, I'm not welcome, and you are the King. Fortunately, I know better and I'm not intimidated by arrogant know it alls like you.

I certainly hope you get the help you need there

See what I mean? Nothing but smart flips and insulting comments.

rfresh737:
I’m just not understanding how to use Mouse.move(). I want to move the mouse pointer to 2 specific spots on the screen and I can’t do it.

I understand each move is relative to its last position and that’s what I just can’t wrap my head around and figure out how to do that. I don’t know why there isn’t a specific x,y function to move the mouse to that x,y point and be done with it.

Ok, now let’s get around to the point. Or, I should say let’s get around to the specific point on the screen, actually. :slight_smile:

All I do when I want to move the mouse to a specific point on the screen is:
– move the mouse to a corner of the screen, and if I hit the edge of the screen don’t let it worry me, just keep on moving anyway, move far enough to get to the corner of any practical size screen
– now that I’m in the corner, move from that position to where you want to be, it will be a specific position, then do something

In this demo, I move to the lower left corner of the screen, then move up and to the right just a little bit, then circle a spot. Then I stop moving the mouse for 10 seconds and repeat. Even if I move the mouse pointer with my regular mouse or trackpad to somewhere else on the screen, it always comes back to the same lower left place on the screen and moves in circle.

/*
  Demo of moving the mouse in a smooth circle without delay
  or blocking of the loop
*/
#include <Mouse.h>
void setup() {
  delay(1000);
}

void loop() {
  static float angle = 100;
  static int x, y;
  static unsigned long counter6;
  static unsigned long counter7;

  if (millis() > 10000 && counter6 < millis() - 10000) {
    counter6 = millis();
    angle = 0.0;
  }

  if (millis() > 20 && counter7 < millis() - 20 && angle < 100) {
    counter7 = millis();
    if (angle < 0.1 ) {
      Mouse.begin();
      // move to the corner of the screen
      for (int counter5 = 0; counter5 <= 100; counter5++) {
        Mouse.move(-127, 127, 0);
      }
      // move to a specific point on the screen
      Mouse.move(20,-30,0);
    }
    // move around a couple of times in a circle
    x = 9 * cos (angle);
    y = 9 * sin (angle);
    angle += .2; // increment the angle
    Mouse.move(x, y, 0);
    if (angle >= 4 * PI) {
      angle = 100;
      Mouse.end();
    }
  }
}

That is not a full sketch, the #include <Mouse.h> is missing.

I’m using a Leonardo board…The sketch I saw showing how to use mouse.move was commented as being tested on a Leonardo and a Duo and he didn’t have that header line in his code.

I added it in my sketch anyway but the compiler complained: No such file or directory.

rfresh737:

That is not a full sketch, the #include <Mouse.h> is missing.

I’m using a Leonardo board…The sketch I saw showing how to use mouse.move was commented as being tested on a Leonardo and a Duo and he didn’t have that header line in his code.

I added it in my sketch anyway but the compiler complained: No such file or directory.

I don’t understand the part about “That is not a full sketch, the #include <Mouse.h> is missing.” Are you talking about the demo sketch I posted (where I did include the “#include <Mouse.h>”? Or are you talking about a post earlier in the discussion?

I am using this sketch with a Leonardo and wrote it with Arduino IDE 1.6.6.

The USB library was changed quite a bit in 1.6.6.
<Mouse.h> is not needed and does not even exist in earlier versions. The same applies to <Keyboard.h>.

dmjlambert:
Ok, now let’s get around to the point. Or, I should say let’s get around to the specific point on the screen, actually. :slight_smile:

All I do when I want to move the mouse to a specific point on the screen is:
– move the mouse to a corner of the screen, and if I hit the edge of the screen don’t let it worry me, just keep on moving anyway, move far enough to get to the corner of any practical size screen
– now that I’m in the corner, move from that position to where you want to be, it will be a specific position, then do something

In this demo, I move to the lower left corner of the screen, then move up and to the right just a little bit, then circle a spot. Then I stop moving the mouse for 10 seconds and repeat. Even if I move the mouse pointer with my regular mouse or trackpad to somewhere else on the screen, it always comes back to the same lower left place on the screen and moves in circle.

/*

Demo of moving the mouse in a smooth circle without delay
  or blocking of the loop
*/
#include <Mouse.h>
void setup() {
  delay(1000);
}

void loop() {
  static float angle = 100;
  static int x, y;
  static unsigned long counter6;
  static unsigned long counter7;

if (millis() > 10000 && counter6 < millis() - 10000) {
    counter6 = millis();
    angle = 0.0;
  }

if (millis() > 20 && counter7 < millis() - 20 && angle < 100) {
    counter7 = millis();
    if (angle < 0.1 ) {
      Mouse.begin();
      // move to the corner of the screen
      for (int counter5 = 0; counter5 <= 100; counter5++) {
        Mouse.move(-127, 127, 0);
      }
      // move to a specific point on the screen
      Mouse.move(20,-30,0);
    }
    // move around a couple of times in a circle
    x = 9 * cos (angle);
    y = 9 * sin (angle);
    angle += .2; // increment the angle
    Mouse.move(x, y, 0);
    if (angle >= 4 * PI) {
      angle = 100;
      Mouse.end();
    }
  }
}

Hmmm…I ran you example and it does do exactly what you say it does…I will have to look over your code and this will take me some as I’m not too advanced in C programming either…but it works…so I think in time this will help me learn how to move the Ardunio mouse around the screen.

Thank you for the help…