Go Down

Topic: USB Absolute Mouse Mode (Read 33033 times) previous topic - next topic


The Arduino IDE 1.0 supports ardruino boards that have a built-in USB capability such as the atmega32u4. This solution has only been tested with adafruit's atmega32u4 breakout board.

Relative Mode
One of the capabilities of these boards is to emulate a mouse. A mouse is typically a relative mode device. If you call Mouse.move(1,1), it will move 1 pixel right and 1 pixel down from where ever it currently is. Mouse.move(-5, 10) will move left 5 and down 10. Note that your board has no way of knowing where the mouse is at any point in time.

Absolute Mode
Fortunately, there is an absolute mode available to the mouse. Once enabled, performing a Mouse.move(1,1) will move the mouse to the very top left corner of the display, no matter where it currently is, or what resolution it is in. Likewise, Mouse.move(100,100) will move it to the bottom right corner of the display. Mouse.move(50,50) is the center (based on the LOGICAL_MAXIMUM in HID.cpp)

The arduino IDE can support absolute mouse mode. However it requires modifying one of the IDE files. This solution is for Arduino 1.0 and has not been tested on prior versions.

1. Kill your Arduino IDE if it is running.
2. Cd to (your arduino directory)/hardware/arduino/cores/arduino directory.
3. Copy your HID.cpp file somewhere safe that you can restore it later if you want to use relative mouse mode again.
4. Copy the attached HID.cpp replacement code below and paste it into your (arduino directory)/hardware/arduino/cores/arduino/ directory.
5. Copy the attached example sketch into the Arduino IDE and compile it.
6. Download the sketch to your atmega32u4 or similar board.
7. Within about 5 seconds, the mouse should start moving around the screen.

Now when you use the normal Mouse.move(x, y) it will use absolute coordinates (vice the default relative coordinates). Note that  LOGICAL_MINIMUM(1) and LOGICAL_MAXIMUM(100) in HID.cpp map to the coordinate space of any display you connect to. So your sketches will always use coordinates between LOGICAL_MINIMUM and LOGICAL_MAXIMUM.

Here's the example sketch that simply moves the mouse left to right, and top to bottom:

Example Sketch
Code: [Select]
void setup(){

void loop(){
   for(int y = 1; y <= 100; y++){
       for(int x = 1; x <= 100; x++){
           Mouse.move(x, y);


Thanks for the post! exactly what I was looking for :)

I had to make some minor changes to make HID.cpp compatible with Ardino 1.0.1 IDE


Here is an example sketch that uses the random fuction to set mouse coordinates.
It uses a "high" input to pin 2 to activate


void setup(){
  pinMode(2, INPUT);

void loop(){
  int x;
  int y;
  if(digitalRead(2) == HIGH){
    for(int z = 1; z <= 10; z++){
      x = random(0, 100);
      y = random(0, 100); 
      Mouse.move(x, y);


Sep 27, 2012, 07:57 am Last Edit: Sep 27, 2012, 07:59 am by yunowa Reason: 1
Thank you!!!
This source is from the computer works fine
But the absolute coordinates from an Android phone does not work.
Android relative coordinates in the source, it works well.
Is there a good way to
I'll give you favor.
I have tested Android phones Galaxy S2
Favor the development environment IDE 1.0.1.
I do not speak English well.I'm sorry.


I was able to use this example to successfully get the mouse to move to a specific location.  It works exactly as expected, until  I follow this with


The mouse moves to the extreme upper left hand corner of the screen before clicking instead of remaining where I put it.
Am I overlooking something?


I was able to get the cursor to stop moving to 0,0 upon Mouse.click();
In the HIP.CPP, I found

Code: [Select]

void Mouse_::click(uint8_t b)
_buttons = b;
_buttons = 0;

and changed it to
Code: [Select]
void Mouse_::click(uint8_t b)
_buttons = b;
// move(0,0,0);
_buttons = 0;
// move(0,0,0);

However, I am still not getting a click in absolute mode.  Any ideas?


Dec 19, 2012, 05:35 pm Last Edit: Dec 19, 2012, 06:12 pm by slechtvalk Reason: 1
i try to use it for a pro micro 5V from SparkFun but it wont work. Arduino gives me this error

D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp: In constructor 'Keyboard_::Keyboard_()':
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:267: error: class 'Keyboard_' does not have any field named '_keyMap'
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp: At global scope:
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:276: error: variable or field 'setKeyMap' declared void
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:276: error: 'KeyMap' was not declared in this scope
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:276: error: 'keyMap' was not declared in this scope
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp: In member function 'virtual size_t Keyboard_::write(uint8_t)':
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:424: error: '_keyMap' was not declared in this scope
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:439: error: 'KEY_MODIFIER_LEFT_SHIFT' was not declared in this scope

edit : same problem at 1.0

edit 2  : now it works with a disabled keyboard function. I still hope for solution so i can also use the keyboard.  When i move the mouse to (100,100) . I don't go to pixel 100,100 but somewhere in the right corner of my screen. Can i use the function that the 100 mean the pixel size?


would you help me how to increase the resolution above mouse.move(100,100); because am using an wide screen laptop it may require mouse.move(120.120);

Carlos mononoke

Hi alls!!

Thanks for your work, it is awesome.

I am trying to move the mouse pointer in one 17´´ Laptop display.
it works but not fit all the display,if the diagonal line is drawed by (1,1) to (100,100) y should need (1,1) to (120,120)

i have triyed change HID.cpp logical_maximun from 0x64 to 0x78 but it didn´t work

Code: [Select]
    0x15, 0x01,                    //     LOGICAL_MINIMUM (1)       <<<< This allows us to talk to any display resolution
    0x25, 0x78,                    //     LOGICAL_MAXIMUM (120)     <<<<  as though it was 100x100 pixels
    0x75, 0x08,                    //     REPORT_SIZE (8)
    0x95, 0x03,                    //     REPORT_COUNT (3)
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)      <<<< This allows us to moveTo absolute positions

and i have tryed change usbcore.cpp device descriptor(...,64,.....) to 78 too,.but it didn´t work
i think it was packetsize variable and maybe.... but not.
Code: [Select]

const DeviceDescriptor USB_DeviceDescriptor =

const DeviceDescriptor USB_DeviceDescriptorA =

Sincerely i have no much idea about what i am doing...
i supose tath somewhere there is a variable that say how many distance can move ore some thing but i have no idea about where can i find it.

someone can help me



This is great, however, mapping to 100 pixels gives a VERY staccato effect to the mouse movement.

What values would you change in the HID file to map to any resolution?


Has anyone managed to find a fix to map the mouse movement to a higher resolution?



Has anyone managed to find a fix to map the mouse movement to a higher resolution?

I did this for Teensy 3.1.  It's in Teensyduino version 1.19 and later.

In setup, you use Mouse.screenSize(width, height) to tell it the coordinate system you want to use.

Then to move the mouse to an absolute position, you use Mouse.moveTo(x, y), where x and y are actual the pixel position on your screen.

Internally, it uses 15 bit numbers for the HID protocol.  The x and y coordinates from Mouse.moveTo() are scaled up, according to whatever you used with Mouse.screenSize().  A LOT of testing went into getting proper integer round-off, so it actually goes to the actual pixel position on both Windows and Macintosh.  The versions of Linux I tested seem to have a bug with accepting absolute mouse coordinates.


Hi Paul, thanks for the reply!  I actually gave this a try before posting here as well, but I keep running into one of two problems.  Either the code does not compile siting a problem with the USBAPI.h file, or the code complies but nothing happens (using the demo code or similar).  From reading up on all this I know I need to replace the HID and USBAPI files with ones that will support absolute positioning, but it seems I can't manage to find a working combination of files and IDE.  I should also mention that I've tried this with Arduino 1.0, 1.0.1, and 1.0.5r2, and that I'm using a Leonardo board.  Any further help or advice would be much appreciated!



Jun 19, 2015, 10:27 pm Last Edit: Jun 19, 2015, 11:15 pm by Elmue
Patrick wrote:
> Mouse.move(-5, 10) will move left 5 and down 10.

This is completely wrong.
How much the mouse finally moves depends on the mouse settings in control panel.
You can set the mouse speed slower or faster and the operating system translates your USB values into different pixel values on the screen. If you pass a value of 10 the mouse may move 4 pixels or 21 pixels on the screen.

And when you enable "Mouse Enhancement" in control panel it becomes even worse: Now the distance depends also on the moving speed.

If you want to position the mouse exactly this can be done ONLY with absolute coordinates.

But there is a big bug in the Linux X11 server that does not accept absolute coordinates from a mouse device.

So if you need a mouse emulation with absolute coordinates to work on all operating systems you cannot use the mouse HID device.

I investigated this topic for several weeks now and made tests on several operating systems.
Finally I found a solution that works on all operating systems, but not with a mouse device.
I use a touch screen device instead.

I did my experiments on a Teensy, not an Arduino, but at the end the USB descriptors are the same.

If you are interested in a universal mouse and touch screen device that can be used with relative AND with absolute coordinates, read my article on Codeproject: A USB HID Keyboard, Mouse, Touchscreen emulator with Teensy

It is a very bad idea to use coordinates from 0 to 100 to position the mouse. The screen resolution is always much higher and you get very imprecise positioning. My code uses values from 0 to 10000 instead which results in 100 times more precision.


Has anyone tried this absolute mouse cordinates and an leonardo board for simulating Resistive touch screen for Anrdoid? That absolute mouse mode works in almost every OS, Ubuntu Linux Windows etc. But absolute mode mouse not working in Android. I bought an lcd screen, also bought a resistive touch panel. I would like to make an android touch screen.

Please look here, look this guy, he had an pro micro and protected this board with a black covering and selling pro micro with 44$ lol. Is this legal?

I tried lots of hid descriptor for android OS but not worked. (HID.cpp hid descriptor)
Any help will be appreciated.

Go Up