0

I'm using my Arduino Leonardo as a Mouse but I'm experimenting a strange behaviour. I put my mouse at (-1, -1) (absolute) coordinates and then I execute this code:

Move.move(com[curr_cmd_id].x, com[curr_cmd_id].y, 0);

where that point is (334, 180) (relative coords) (and I checked it through Serial console)

Now, the pointer is actually moving to the right but then it stops. Let's say it reaches the point (334, 0).

Then I tried:

Mouse.move(com[curr_cmd_id].x, 0, 0);
delay(500);
Mouse.move(0, com[curr_cmd_id].y, 0);

but still the same behaviour.

Then finally I tried this one (which it actually solves my problem but it's a dirty solution):

int i;
for(i=0; i<com[curr_cmd_id].x; i++){
  Mouse.move(1,0,0);
  delay(1);
}
for(i=0; i<com[curr_cmd_id].y; i++){
  Mouse.move(0,1,0);
  delay(1);
}

Could anybody help me in understanding what's going on here? How can I achieve a simple and direct relative movement for my pointer? Thanks you in advance!!

4
  • So it's only moving to the right, and not up?
    – Gerben
    Aug 21, 2014 at 15:38
  • Only on the right and not down (considering the coordinates I expect to go down with positive y)
    – Cob013
    Aug 21, 2014 at 16:43
  • It looks like maybe there's something filtering a maximum "jump" in the mouse position. What happens if your increment your for loop by more than 1 - can you experimentally discover a limit? Aug 22, 2014 at 15:18
  • why result is so diffrent between : moveMouse(127, 0, 0); and for (i=1; i<= 127; i++) { moveMouse(1, 0, 0);} ?
    – user16773
    Jan 27, 2016 at 13:16

1 Answer 1

1

I struggled with this too until I realized this

Parameters

xVal: amount to move along the x-axis - signed char

yVal: amount to move along the y-axis - signed char

wheel: amount to move scroll wheel - signed char

Note it says signed char.

That means the value you pass to the Mouse.move is a signed 8-bit. (-128 to 127)

My work around is this below:

void moveMouse(int x, int y, int w){
  x = (int)x / 1.59; //Adjust these two values accordingly. I found out that 100, 100 does 
  y = (int)y / 1.58; //not move 100, 100. It moves 159, 158 for me.

  while(x!=0 || y!=0 || w!=0){
    int moveX, moveY, moveW;
    if(x > 0){
      if(x >= 100){
        moveX = 100;
      }else{
        moveX = x;
      }
    }else if (x < 0){
      if(x <= -100){
        moveX = -100;
      }else{
        moveX = x;
      }
    }else{
      moveX = 0;
    }

    if(y > 0){
      if(y >= 100){
        moveY = 100;
      }else{
        moveY = y;
      }
    }else if (y < 0){
      if(y <= -100){
        moveY = -100;
      }else{
        moveY = y;
      }
    }else{
      moveY = 0;
    }

    if(w > 0){
      if(w >= 127){
        moveW = 127;
      }else{
        moveW = w;
      }
    }else if (w < 0){
      if(w <= -128){
        moveW = -128;
      }else{
        moveW = w;
      }
    }else{
      moveW = 0;
    }

    x = x - moveX;
    y = y - moveY;
    w = w - moveW;

    Mouse.move(moveX, moveY, moveW);
  } 
1
  • Same answer in 5 lines: void MouseMove(int x, int y) { for(int i=0; (x>0)?(i<x/100):(i>x/100); (x>0)?(i++):(i--)) Mouse.move((x>0)?(100):(-100), 0); for(int j=0; (y>0)?(j<y/100):(j>y/100); (y>0)?(j++):(j--)) Mouse.move(0, (y>0)?(100):(-100)); Mouse.move(x%100, y%100); }
    – sbrm1
    Aug 12, 2019 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.