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

释放双眼,带上耳机,听听看~!
这个例子示范了怎么用鼠标键盘库文件。5个即时开关作为你光标的定向按键。当一个按键按下,你屏幕上的光标将会移动,而且和有方向的字母相联系的按下按键,会被发送到电脑。一旦你编译好Leonardo, Micro or Due,并且连好线,打开你的文本编译器来看结果。

简介

  • 这个例子示范了怎么用鼠标键盘库文件。5个即时开关作为你光标的定向按键。当一个按键按下,你屏幕上的光标将会移动,而且和有方向的字母相联系的按下按键,会被发送到电脑。一旦你编译好Leonardo, Micro or Due,并且连好线,打开你的文本编译器来看结果。
  • 注意:当你用鼠标键盘库函数时,Arduino会接管你的电脑键盘!为了确保你没有失去对电脑的控制同时运行这个函数,确定在你调用Mouse.move()前,启动一个可靠的控制系统。

硬件要求

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

软件要求

  • Arduino IDE

电路

  • 分别连接按键的一端到开发板的pin2,3,4,5,和6上。连接另一端到5V电源。为这些开关各提供一个下拉电阻,开关通过电阻连接到地。
  • 一旦你编译好的你的开发板,插上USB线并打开一个文本编译器。连接你的开发板和电脑,并且在移动光标时按下按键写入文件。

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

原理图

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

样例代码

/*
  KeyboardAndMouseControl

 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 "Keyboard.h"
#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;

void setup() { // initialize the buttons' inputs:
  pinMode(upButton, INPUT);
  pinMode(downButton, INPUT);
  pinMode(leftButton, INPUT);
  pinMode(rightButton, INPUT);
  pinMode(mouseButton, INPUT);

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

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

  // use the pushbuttons to control the keyboard:
  if (digitalRead(upButton) == HIGH) {
    Keyboard.write('u');
  }
  if (digitalRead(downButton) == HIGH) {
    Keyboard.write('d');
  }
  if (digitalRead(leftButton) == HIGH) {
    Keyboard.write('l');
  }
  if (digitalRead(rightButton) == HIGH) {
    Keyboard.write('r');
  }
  if (digitalRead(mouseButton) == HIGH) {
    Keyboard.write('m');
  }

}

给TA打赏
共{{data.count}}人
人已打赏
ArduinoArduino-官方内置动态

Arduino内置教程-USB-键盘串口

2019-1-6 20:10:13

ArduinoArduino-官方内置动态

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

2019-1-6 22:20:46

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
'); })();