< >
Home » Arduino内置教程 » Arduino内置教程-USB-鼠标按键控制

Arduino内置教程-USB-鼠标按键控制

鼠标按键控制

  • 用鼠标库文件,你可以用一个Arduino Leonardo, Micro, 或者 Due来控制一个电脑的光标显示。这个例子示范了怎么用5个按键来移动屏幕上的光标。4个按键时方向(上下左右),1个是鼠标左键。

  • 光标移动是有关联的。每次读到输入时,光标的位置就会根据它当前位置更新。

  • 无论什么时候按下方向按键,Arduino都会移动鼠标,匹配一个高电平到范围是5的合适的方向。

  • 第5个按键用来控制鼠标左键按下。当按下第5个按键时,开发板发送一个按下到电脑。当按键松开时,电脑会意识到这件事。

  • 注意:当你用Mouse.move()命令时,Arduino会接管你的电脑键盘!为了确保你没有失去对电脑的控制同时运行这个函数,确定在你调用Mouse.move()前,启动一个可靠的控制系统。这个程序只会在你按下按键时更新光标位置。

硬件要求

  • Arduino Leonardo, Micro or Due开发板
  • 5 即时按键
  • 5 10k ohm电阻
  • 连接线
  • 面包板

电路

  • 通过一个 micro-USB 线连接你的开发板到你的电脑。按键连接到数字输入从pin2到pin6。确保你用上10k ohm下拉电阻。

请输入图片描述
图由 Fritzing 软件绘制
请输入图片描述

样例代码

/*
  ButtonMouseControl

 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.

 created 15 Mar 2012
 modified 27 Mar 2012
 by Tom Igoe

 this code is in the public domain

 */

#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 the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT);
    }
  }

  // a delay so the mouse doesn't move too fast:
  delay(responseDelay);
}

[Get Code]

更多

  • Mouse.click()
  • Mouse.move()
  • Mouse.press()
  • Mouse.release()
  • Mouse.isPressed()
  • KeyboardLogout - 利用按键命令注销当前使用者
  • KeyboardMessage - 当一个按键被按下,发送一个文本字符串。
  • KeyboardReprogram - 在Arduino IDE上打开一个新窗口,用简单的跑马灯程序重新编译Leonardo
  • KeyboardSerial - 从串口里读取一个字节,然后返回一个键值。
  • KeyboardAndMouseControl - 在一个程序里示范鼠标和键盘命令
  • ButtonMouseControl - 通过5个按键控制光标行动
  • JoystickMouseControl - 当按键被按下时,通过一个操纵杆来控制电脑光标的行动

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: arduino内置教程, arduino鼠标按键控制