PatrickRyan

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 ModeOne 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 ModeFortunately, 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.

Procedure: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:

yunowa

Thank you!!!This source is from the computer works fineBut the absolute coordinates from an Android phone does not work.Android relative coordinates in the source, it works well.Is there a good way toI'll give you favor.I have tested Android phones Galaxy S2Favor the development environment IDE 1.0.1.I do not speak English well.I'm sorry.

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 voidD:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:276: error: 'KeyMap' was not declared in this scopeD:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:276: error: 'keyMap' was not declared in this scopeD:\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 scopeD:\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?

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.

Rocket_Man_Jeff

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!

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.

P.D.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?

https://www.youtube.com/watch?v=vLBP1v2OHQQI tried lots of hid descriptor for android OS but not worked. (HID.cpp hid descriptor)Any help will be appreciated.