0

How do I move more than 128 pixels when using Arduino Leonardo's Mouse.move? Is it possible to modify the header file or use a different library? I used the MouseTo library, but it wasn't the move I wanted. We want to move as fast as possible to the relative coordinates, not as one pixel. I've searched almost every community, but I haven't. Give them a hand.

1

The USB HID protocol for mouse is the limitation on sending more then a byte sized value for the mouse movement in one event.

You can't create a faster USB mouse. Repeat the call of the move() function. Of course you can use a function to call move() repeatedly to move to target destination.

void mouseMove(long x, long y) {
  long max = max(abs(x), abs(y));
  int count = (int) (max / 127);
  signed char stepX = x / (count + 1);
  signed char stepY = y / (count + 1);
  for (int i = 0; i < count; i++) {
    Mouse.move(stepX, stepY);
  }
  signed char resX = x - (stepX * count);
  signed char resY = y - (stepY * count);
  if (resX != 0 || resY != 0) {
    Mouse.move(resX, resY);
  }
}
  • There were times when the exact value was not available when I called multiple times. Anyway, thank you for your answer. – bdconfig May 19 at 20:59
0

For X and Y values between -64 and 64, consider multiplying the X and Y values you are passing to the method mouse.move() by 2. In the above linked page, the example call to mouse.move() looks like this:

mouse.move(xVal, yVal, wheel)

Consider writing it like this for twice the change in either direction:

mouse.move((2 * xVal), (2 * yVal), wheel)

For values less then -128 or greater than 128, consider calling the method multiple times instead of once for each time it is desired to move the mouse.

A more difficult approach: You may also consider re-writing or adding to the Arduino mouse driver. It appears the Arduino mouse driver is located here. As this is C++ you will need to change the definition of the "mouse" method as well as how you handle int as opposed to char sized variables. In this case you can take advantage of the C++ Method Overloading feature. That is, you can have 2 methods of the same name with different parameter types. C++ will treat them as two individual methods. So the original mouse.move method can remain untouched. While the new mouse.move method will be able to handle type singed int instead of type singed char values.

  • Wouldn't the multiplying approach let the parameters overflow? The explanation after that seems better to me, calling the method multiple times or changing the library. – chrisl May 19 at 19:12
  • what? the unsigned chars (-127 to 127) are defined in the HID event. how would you reimplement that? – Juraj May 19 at 20:15
  • I see. Your answer was helpful to me even if it was not possible. – bdconfig May 19 at 21:00
  • @juraj, If the HID definition limit mouse movement the library can abstract this from the Arduino sketch by sending multiple HID commands should the value passed to the library be larger than allowed by the HID interface. So a library call using ints instead of chars can still be created. – st2000 May 19 at 23:46
  • @chrisl, yes, I'll rephrase the answer. – st2000 May 19 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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