USB Absolute Mouse Mode

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.

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:

Example Sketch

void setup(){
}

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

HID.cpp (11.7 KB)

AbsoluteMouseMode.ino (177 Bytes)

1 Like

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

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

HID.cpp (14.5 KB)

1 Like

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){
Mouse.begin();
for(int z = 1; z <= 10; z++){
x = random(0, 100);
y = random(0, 100);
Mouse.move(x, y);
delay(100);
}
Mouse.end();
}
}

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

Mouse.click();

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

void Mouse_::click(uint8_t b)
{
	_buttons = b;
  	move(0,0,0);
	_buttons = 0;
  	move(0,0,0);
}

and changed it to

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?

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);

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

#ifdef ABSOLUTE_MOUSE_MODE
    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
#else

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.

	DEVICE DESCRIPTOR
const DeviceDescriptor USB_DeviceDescriptor =
	D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);

const DeviceDescriptor USB_DeviceDescriptorA =
	D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);

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

Thanks!!!!

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?

Thanks!
-Jeff

Rocket_Man_Jeff:
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!

Thanks,
-Jeff

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

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?

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

Absolute mouse mode for Digispark AtTiny85, range -127/+127

Path to file (on Linux):
.arduino15/packages/digistump/hardware/avr/1.6.7/libraries/DigisparkMouse/DigiMouse.h

(comments are gone due size limitations here)

#ifndef __DigiMouse_h__
#define __DigiMouse_h__

#define REPORT_SIZE 4

#include <Arduino.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
#include <string.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/wdt.h>

#include "usbdrv.h"
//#include "devdesc.h"
#include "oddebug.h"
#include "usbconfig.h"

static const uchar *rt_usbHidReportDescriptor = NULL;
static uchar rt_usbHidReportDescriptorSize = 0;
static const uchar *rt_usbDeviceDescriptor = NULL;
static uchar rt_usbDeviceDescriptorSize = 0;

#define MOUSEBTN_LEFT_MASK		0x01
#define MOUSEBTN_RIGHT_MASK		0x02
#define MOUSEBTN_MIDDLE_MASK	0x04

typedef uint8_t byte;

static unsigned char last_built_report[REPORT_SIZE];
static unsigned char last_sent_report[REPORT_SIZE];

uchar		 reportBuffer[REPORT_SIZE];

#define DIGIMOUSE_DEFAULT_REPORT_INTERVAL 20
static unsigned char must_report = 0;
static unsigned char idle_rate = DIGIMOUSE_DEFAULT_REPORT_INTERVAL / 4; // in units of 4ms
static unsigned long last_report_time = 0;


//#define ABSOLUTE_MOUSE_MODE


char usb_hasCommed = 0;

const PROGMEM unsigned char mouse_usbHidReportDescriptor[] = { /* USB report descriptor */
0x05, 0x01,
0x09, 0x02,
0xa1, 0x01,
0x09, 0x01,
0xa1, 0x00,
0x05, 0x09,
0x19, 0x01,
0x29, 0x03,
0x15, 0x00,
0x25, 0x01,
0x95, 0x03,
0x75, 0x01,
0x81, 0x02,
0x95, 0x01,
0x75, 0x05,
0x81, 0x01,
0x05, 0x01,
0x09, 0x30,
0x09, 0x31,

#ifdef ABSOLUTE_MOUSE_MODE
0x15, 0x81,	
0x25, 0x7f,	
0x75, 0x08, 
0x95, 0x03, 
0x81, 0x02, 
#else
0x15, 0x81,	
0x25, 0x7f,	
0x75, 0x08,	
0x95, 0x02,	
0x81, 0x06,	
#endif

0x09, 0x38,	
0x95, 0x01,	
0x81, 0x06,	

0xc0,							
0xc0								
};


#define USBDESCR_DEVICE					1

const unsigned char usbDescrDevice[] PROGMEM = {		/* USB device descriptor */
18,					/* sizeof(usbDescrDevice): length of descriptor in bytes */
USBDESCR_DEVICE,		/* descriptor type */
0x01, 0x01, /* USB version supported */
USB_CFG_DEVICE_CLASS,
USB_CFG_DEVICE_SUBCLASS,
0,					/* protocol */
8,					/* max packet size */
USB_CFG_VENDOR_ID,	/* 2 bytes */
USB_CFG_DEVICE_ID,	/* 2 bytes */
USB_CFG_DEVICE_VERSION, /* 2 bytes */
#if USB_CFG_VENDOR_NAME_LEN
1,		
#els
0,		
#endif
#if USB_CFG_DEVICE_NAME_LEN
2,		
#els
0,		
#endif
#if USB_CFG_SERIAL_NUMBER_LENGTH
3,		
#els
0,		
#end
1,		
};


void buildReport(unsigned char *reportBuf) {
if (reportBuf != NULL) {
memcpy(reportBuf, last_built_report, REPORT_SIZE);
}

memcpy(last_sent_report, last_built_report, REPORT_SIZE); 
}

void clearMove() {
// because we send deltas in movement, so when we send them, we clear them
last_built_report[1] = 0;
last_built_report[2] = 0;
last_built_report[3] = 0;
last_sent_report[1] = 0;
last_sent_report[2] = 0;
last_sent_report[3] = 0;
}






class DigiMouseDevice {
public:
DigiMouseDevice () {

rt_usbHidReportDescriptor = mouse_usbHidReportDescriptor;
rt_usbHidReportDescriptorSize = sizeof(mouse_usbHidReportDescriptor);
rt_usbDeviceDescriptor = usbDescrDevice;
rt_usbDeviceDescriptorSize = sizeof(usbDescrDevice);
}

void begin() {
cli();
usbDeviceDisconnect();
_delay_ms(200);
usbDeviceConnect();	

usbInit();

sei();
last_report_time = millis();
}



void refresh() {
update();
}

void poll() {
update();
}


void update() {
usbPoll();

unsigned long time_since_last_report = millis() - last_report_time;
if (time_since_last_report >= (idle_rate * 4 /* in units of 4ms - usb spec stuff */)) {
last_report_time += idle_rate * 4;
must_report = 1;
}

if (memcmp(last_built_report, last_sent_report, REPORT_SIZE)) {
must_report = 1;
}

if (must_report) {
if (usbInterruptIsReady()) {
must_report = 0;
buildReport(reportBuffer); // put data into reportBuffer
clearMove(); // clear deltas
usbSetInterrupt(reportBuffer, REPORT_SIZE);
}
}
}

void delay(long milli) {
unsigned long last = millis();
while (milli > 0) {
unsigned long now = millis();
milli -= now - last;
last = now;
update();
}
}

void moveX(char deltaX)	{
if (deltaX == -128) deltaX = -127;
last_built_report[1] = *(reinterpret_cast<unsigned char *>(&deltaX));
}

void moveY(char deltaY) {
if (deltaY == -128) deltaY = -127;
last_built_report[2] = *(reinterpret_cast<unsigned char *>(&deltaY));
}

void scroll(char deltaS)	{
if (deltaS == -128) deltaS = -127;
last_built_report[3] = *(reinterpret_cast<unsigned char *>(&deltaS));	 
}

void move(char deltaX, char deltaY, char deltaS) {
if (deltaX == -128) deltaX = -127;
if (deltaY == -128) deltaY = -127;
if (deltaS == -128) deltaS = -127;
last_built_report[1] = *(reinterpret_cast<unsigned char *>(&deltaX));
last_built_report[2] = *(reinterpret_cast<unsigned char *>(&deltaY));
last_built_report[3] = *(reinterpret_cast<unsigned char *>(&deltaS));
}

void move(char deltaX, char deltaY, char deltaS, char buttons) {
if (deltaX == -128) deltaX = -127;
if (deltaY == -128) deltaY = -127;
if (deltaS == -128) deltaS = -127;
last_built_report[0] = buttons;
last_built_report[1] = *(reinterpret_cast<unsigned char *>(&deltaX));
last_built_report[2] = *(reinterpret_cast<unsigned char *>(&deltaY));
last_built_report[3] = *(reinterpret_cast<unsigned char *>(&deltaS));
}

void rightClick(){
last_built_report[0] = MOUSEBTN_RIGHT_MASK;
}

void leftClick(){
last_built_report[0] = MOUSEBTN_RIGHT_MASK;
}

void middleClick(){
last_built_report[0] = MOUSEBTN_RIGHT_MASK;
}

void setButtons(unsigned char buttons) {
last_built_report[0] = buttons;
}

void setValues(unsigned char values[]) {
memcpy(last_built_report, values, REPORT_SIZE);
}

//private: TODO: Make friend?
// what does this even mean? -- Bluebie
};

// create the global singleton DigiMouse
DigiMouseDevice DigiMouse = DigiMouseDevice();


#ifdef __cplusplus
extern "C"{
#endif 
// USB_PUBLIC uchar usbFunctionSetup

uchar usbFunctionSetup(uchar data[8]) {
usbRequest_t *rq = (usbRequest_t *)data;

usbMsgPtr = reportBuffer;

if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {		/* class request type */
return REPORT_SIZE;
} else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
usbMsgPtr = &idle_rate;
return 1;
} else if (rq->bRequest == USBRQ_HID_SET_IDLE) {
idle_rate = rq->wValue.bytes[1];
}
} else {
}
return 0;
}

uchar	usbFunctionDescriptor(struct usbRequest *rq) {
if ((rq->bmRequestType & USBRQ_TYPE_MASK) != USBRQ_TYPE_STANDARD) {
return 0;
}

if (rq->bRequest == USBRQ_GET_DESCRIPTOR) {
switch (rq->wValue.bytes[1]) {
case USBDESCR_DEVICE:
usbMsgPtr = rt_usbDeviceDescriptor;
return rt_usbDeviceDescriptorSize;
break;

case USBDESCR_HID_REPORT:
usbMsgPtr = rt_usbHidReportDescriptor;
return rt_usbHidReportDescriptorSize;
break;
}
}

return 0;
}

#ifdef __cplusplus
} // extern "C"
#endif


#endif // __DigiMouse_h__

Hey there,
When i try to upload my code it shows an error...
Arduino: 1.8.1 (Mac OS X), Board: "Arduino Leonardo"

/private/var/folders/hn/flfx2mnd237974bkycxx4cyr0000gn/T/AppTranslocation/F9EEF18B-0923-4A05-BE90-F6FC217A14A7/d/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/HID/src/HID.cpp:19:22: fatal error: platform.h: No such file or directory
#include <platform.h>
^
compilation terminated.
exit status 1
Error compiling for board Arduino Leonardo.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Please Help me out ASAP I have a project to submit.
Thank you

This is great, however you don't list how to find HID.cpp

I've searched everywhere in all of my Arduino directories and HID.cpp is not listed anywhere at all.

Any help would be greatly appreciated.

If using Linux and maybe Mac, use the find command line util.

Change to top of home directory

$ cd
$ find . -name HID.cpp
./.arduino15/packages/arduino/hardware/samd/1.6.16/libraries/HID/HID.cpp
./arduino-1.8.4/hardware/arduino/avr/libraries/HID/src/HID.cpp

The first is the one for SAMD ARM (Zero) boards. The second is for AVR (Leonardo) boards.

arduino 1.8.5????