22年的时候尝试过用arduino模拟usb键盘实现自动向手机输入一些字符串。见博文《利用arduino uno模拟手机hid键盘》,这几天刷视频的时候想实现一个自动上滑的效果,就想看下手头的arduino uno能否模拟usb鼠标。官方的mouse库仅适用于arduino leonardo以及nano等,所以想实现这个效果,还是需要另辟蹊径,好在也有大神用类似模拟键盘的方式,给出了mouse的hex文件,以下是下载链接,需要注意的是,这个案例仅适合安卓手机,ios对于接入设备有严格的认证机制,插入模拟鼠标后完全没有响应:
百度网盘
示例程序
/* Arduino USB Mouse HID demo */
/* Author: Darran Hunt
* Release into the public domain.
*/
struct {
uint8_t buttons;
int8_t x;
int8_t y;
int8_t wheel; /* Not yet implemented */
} mouseReport;
uint8_t nullReport[4] = { 0, 0, 0, 0 };
void setup();
void loop();
void setup()
{
Serial.begin(9600);
delay(200);
}
/* Move the mouse in a clockwise square every 5 seconds */
void loop()
{
int ind;
delay(1000);
mouseReport.buttons = 0;
mouseReport.x = 0;
mouseReport.y = 0;
mouseReport.wheel = 0;
mouseReport.x = -2;
for (ind=0; ind<20; ind++) {
Serial.write((uint8_t *)&mouseReport, 4);
Serial.write((uint8_t *)&nullReport, 4);
}
mouseReport.x = 0;
mouseReport.y = -2;
for (ind=0; ind<20; ind++) {
Serial.write((uint8_t *)&mouseReport, 4);
Serial.write((uint8_t *)&nullReport, 4);
}
mouseReport.x = 2;
mouseReport.y = 0;
for (ind=0; ind<20; ind++) {
Serial.write((uint8_t *)&mouseReport, 4);
Serial.write((uint8_t *)&nullReport, 4);
}
mouseReport.x = 0;
mouseReport.y = 2;
for (ind=0; ind<20; ind++) {
Serial.write((uint8_t *)&mouseReport, 4);
Serial.write((uint8_t *)&nullReport, 4);
}
}美中不足
遗憾的是,这个hex暂未实现鼠标滚轮的模拟。想实现一个向上滚动的动作,可以考虑以向下空滑->按下鼠标左键->保持按键->向上滑动的方式来代替,这里提供一个样例程序:
struct {
uint8_t buttons;
int8_t x;
int8_t y;
int8_t wheel;
} mouseReport;
uint8_t nullReport0[4] = { 0, 0, 0, 0 };
uint8_t nullReport1[4] = { 1, 0, 0, 0 };
void setup() {
Serial.begin(9600);
delay(200);
randomSeed(analogRead(0));
}
void loop() {
mouseReport.x = 0;
mouseReport.y = 100;
Serial.write((uint8_t *)&mouseReport, 4);
Serial.write((uint8_t *)&nullReport0, 4);
mouseReport.buttons = 1;
mouseReport.x = 0;
mouseReport.y = -100;
Serial.write((uint8_t *)&mouseReport, 4);
Serial.write((uint8_t *)&nullReport1, 4);
mouseReport.x = 0;
mouseReport.y = -0;
mouseReport.buttons = 0;
Serial.write((uint8_t *)&mouseReport, 4);
Serial.write((uint8_t *)&nullReport0, 4);
int randomDelay = random(5000, 8001);
delay(randomDelay);
}循环版本
实际测试中发现不添加停顿容易产生偏移与上滑失败的现象,重新修改了一下程序,在安卓上测试通过,但ios无法识别到这个设备:
struct {
uint8_t buttons;
int8_t x;
int8_t y;
int8_t wheel;
} mouseReport;
uint8_t nullReport0[4] = { 0, 0, 0, 0 };
uint8_t nullReport1[4] = { 1, 0, 0, 0 };
void setup() {
Serial.begin(9600);
delay(200);
randomSeed(analogRead(0));
}
void loop() {
mouseReport.x = 0;
mouseReport.y = 100;
for(int i=1;i<=6;i++){
Serial.write((uint8_t *)&mouseReport, 4);
Serial.write((uint8_t *)&nullReport0, 4);
delay(20);
}
mouseReport.buttons = 1;
mouseReport.x = 0;
mouseReport.y = -100;
for(int i=1;i<=6;i++){
Serial.write((uint8_t *)&mouseReport, 4);
Serial.write((uint8_t *)&nullReport1, 4);
delay(20);
}
mouseReport.x = 0;
mouseReport.y = -50;
mouseReport.buttons = 0;
Serial.write((uint8_t *)&mouseReport, 4);
Serial.write((uint8_t *)&nullReport0, 4);
int randomDelay = random(2000, 3001);
delay(randomDelay);
}

