Arduino 简明教程
Arduino - Mouse Button Control
使用鼠标库,你可以用 Arduino Leonardo、Micro 或 Due 控制计算机屏幕上的光标。
Using the Mouse library, you can control a computer’s onscreen cursor with an Arduino Leonardo, Micro, or Due.
此特定示例使用五个按钮来移动屏幕光标。四个按钮是方向性的(上、下、左、右),还有一个用于左键单击。从 Arduino 移动光标始终是相对的。每次读取输入时,光标的位置都相对于其当前位置更新。
This particular example uses five pushbuttons to move the onscreen cursor. Four of the buttons are directional (up, down, left, right) and one is for a left mouse click. Cursor movement from Arduino is always relative. Every time an input is read, the cursor’s position is updated relative to its current position.
只要按下方向按钮中的一个,Arduino 就会移动鼠标,将一个高输入映射到适当方向的 5 个范围。
Whenever one of the directional buttons is pressed, Arduino will move the mouse, mapping a HIGH input to a range of 5 in the appropriate direction.
第五个按钮用于控制鼠标的左键单击。释放按钮时,计算机将识别该事件。
The fifth button is for controlling a left-click from the mouse. When the button is released, the computer will recognize the event.
Components Required
您将需要以下组件:
You will need the following components −
-
1 × Breadboard
-
1 × Arduino Leonardo, Micro or Due board
-
5 × 10k ohm resistor
-
5 × momentary pushbuttons
Procedure
按照电路图,按照以下图片所示将组件连接到面包板上。
Follow the circuit diagram and hook up the components on the breadboard as shown in the image below.

Sketch
在计算机上打开 Arduino IDE 软件。使用 Arduino 语言进行编码将控制您的电路。单击“新建”以新建一个草图文件。
Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Open a new sketch File by clicking New.
以下示例中,需要使用 Arduino IDE 1.6.7
For this example, you need to use Arduino IDE 1.6.7

Arduino Code
/*
Button Mouse Control
For Leonardo and Due boards only .Controls the mouse from
five pushbuttons on an Arduino Leonardo, Micro or Due.
Hardware:
* 5 pushbuttons attached to D2, D3, D4, D5, D6
The mouse movement is always relative. This sketch reads
four pushbuttons, and uses them to set the movement of the mouse.
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.
*/
#include "Mouse.h"
// set pin numbers for the five buttons:
const int upButton = 2;
const int downButton = 3;
const int leftButton = 4;
const int rightButton = 5;
const int mouseButton = 6;
int range = 5; // output range of X or Y movement; affects movement speed
int responseDelay = 10; // response delay of the mouse, in ms
void setup() {
// initialize the buttons' inputs:
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
pinMode(mouseButton, INPUT);
// initialize mouse control:
Mouse.begin();
}
void loop() {
// read the buttons:
int upState = digitalRead(upButton);
int downState = digitalRead(downButton);
int rightState = digitalRead(rightButton);
int leftState = digitalRead(leftButton);
int clickState = digitalRead(mouseButton);
// calculate the movement distance based on the button states:
int xDistance = (leftState - rightState) * range;
int yDistance = (upState - downState) * range;
// if X or Y is non-zero, move:
if ((xDistance != 0) || (yDistance != 0)) {
Mouse.move(xDistance, yDistance, 0);
}
// if the mouse button is pressed:
if (clickState == HIGH) {
// if the mouse is not pressed, press it:
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
} else { // else the mouse button is not pressed:
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}
// a delay so the mouse does not move too fast:
delay(responseDelay);
}