天天看點

Arduino Leonardo 模拟鍵盤

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

繼續閱讀