欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Arduino Leonardo 模拟键盘

程序员文章站 2022-07-14 08:23:56
...

UNO  和 nano 不能模拟键盘输入 请使用 Leonardo 

 

#include "Keyboard.h"
int PlayerPin[] = {A0, A1, A2, A3, A4, A5, 0, 1, 2, 3,       4, 5, 6, 7, 8, 9,   10, 11,  12,  13};
// 玩家1            W   A    S   D  F    G  I  J  K  F1  玩家2上 左  下  右 3 NUM1 NUM2 NUM4 NUM5 F1
// 键盘的ASCALL码
int keyBoardAscall[] = {87,65,83,68,70,71,73,74,75,194,   218,216,217,215,51,49,50,52,53,194};
void setup()
{
  Serial.begin(9600);
  Keyboard.begin();
  for (int i = 0; i < 20; i++) {
    pinMode(PlayerPin[i], INPUT_PULLUP);
  }
}

void loop()
{
  //Serial.println(digitalRead(PlayerPin[0]));

  for (int i = 0; i< 20; i++){
    if(digitalRead(PlayerPin[i]) == 0){
      Keyboard.press(keyBoardAscall[i]); 
    }
  }
   
  Keyboard.releaseAll();
  delay(100);
}
                                 

Arduino Leonardo 模拟键盘

相关标签: Arduino