2012-03-27 21:01:07 +02:00
|
|
|
/*
|
|
|
|
KeyboardAndMouseControl
|
2012-10-21 18:36:54 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
Controls the mouse from five pushbuttons on an Arduino Leonardo, Micro or Due.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
Hardware:
|
2017-07-12 22:18:23 +02:00
|
|
|
- five pushbuttons attached to D2, D3, D4, D5, D6
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
The mouse movement is always relative. This sketch reads
|
|
|
|
four pushbuttons, and uses them to set the movement of the mouse.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
WARNING: When you use the Mouse.move() command, the Arduino takes
|
|
|
|
over your mouse! Make sure you have control before you use the mouse commands.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
created 15 Mar 2012
|
|
|
|
modified 27 Mar 2012
|
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
This example code is in the public domain.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/KeyboardAndMouseControl
|
2017-07-15 00:35:58 +02:00
|
|
|
*/
|
2012-03-27 21:01:07 +02:00
|
|
|
|
2015-07-15 16:05:28 +02:00
|
|
|
#include "Keyboard.h"
|
|
|
|
#include "Mouse.h"
|
|
|
|
|
2012-03-27 21:01:07 +02:00
|
|
|
// set pin numbers for the five buttons:
|
2013-10-21 09:58:40 +02:00
|
|
|
const int upButton = 2;
|
|
|
|
const int downButton = 3;
|
2012-03-27 21:01:07 +02:00
|
|
|
const int leftButton = 4;
|
|
|
|
const int rightButton = 5;
|
|
|
|
const int mouseButton = 6;
|
|
|
|
|
|
|
|
void setup() { // initialize the buttons' inputs:
|
2013-10-21 09:58:40 +02:00
|
|
|
pinMode(upButton, INPUT);
|
|
|
|
pinMode(downButton, INPUT);
|
|
|
|
pinMode(leftButton, INPUT);
|
|
|
|
pinMode(rightButton, INPUT);
|
2012-03-27 21:01:07 +02:00
|
|
|
pinMode(mouseButton, INPUT);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-03-27 21:01:07 +02:00
|
|
|
Serial.begin(9600);
|
|
|
|
// initialize mouse control:
|
|
|
|
Mouse.begin();
|
|
|
|
Keyboard.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// use serial input to control the mouse:
|
|
|
|
if (Serial.available() > 0) {
|
|
|
|
char inChar = Serial.read();
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
switch (inChar) {
|
|
|
|
case 'u':
|
|
|
|
// move mouse up
|
|
|
|
Mouse.move(0, -40);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
// move mouse down
|
|
|
|
Mouse.move(0, 40);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
// move mouse left
|
|
|
|
Mouse.move(-40, 0);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
// move mouse right
|
|
|
|
Mouse.move(40, 0);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
// perform mouse left click
|
|
|
|
Mouse.click(MOUSE_LEFT);
|
|
|
|
break;
|
2012-03-27 21:01:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the pushbuttons to control the keyboard:
|
|
|
|
if (digitalRead(upButton) == HIGH) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Keyboard.write('u');
|
2012-03-27 21:01:07 +02:00
|
|
|
}
|
|
|
|
if (digitalRead(downButton) == HIGH) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Keyboard.write('d');
|
2012-03-27 21:01:07 +02:00
|
|
|
}
|
|
|
|
if (digitalRead(leftButton) == HIGH) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Keyboard.write('l');
|
2012-03-27 21:01:07 +02:00
|
|
|
}
|
|
|
|
if (digitalRead(rightButton) == HIGH) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Keyboard.write('r');
|
2012-03-27 21:01:07 +02:00
|
|
|
}
|
|
|
|
if (digitalRead(mouseButton) == HIGH) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Keyboard.write('m');
|
2012-03-27 21:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|