Arduino如何使用KEY_LEFT_CTRL

时间:2015-11-10 04:45:52

标签: arduino usb keyboard-events ctrl

我试图在arduino板上制作一个简单的按钮,按下时执行ctrl + z命令(用于撤消)。我知道Arduino中ctrl的修饰符是KEY_LEFT_CTRL

我的按钮结构如下,发送单个字母工作正常,但KEY_LEFT_CTRL似乎不适用。我尝试过几次尝试,当前的代码编译在下面,但只是发送Z

enum { sw0=3, sw1=4, sw2=5, sw3=6, sw4=7, sw5=8, sw6=9, sw7=10}; // Switchbutton lines
enum { nSwitches=8, bounceMillis=42}; // # of switches; debounce delay
struct ButtonStruct {
  unsigned long int bounceEnd;  // Debouncing-flag and end-time
  // Switch number, press-action, release-action, and prev reading
  byte swiNum, swiActP, swiActR, swiPrev;
};

struct ButtonStruct buttons[nSwitches] = {
  {0, sw0, 'A'}, 
  {0, sw1, 'B'}, 
  {0, sw2, 'C'}, 
  {0, sw3, 'D'},
  {0, sw4, 'E'},
  {0, sw5, 'F'},
  {0, sw6, 'G'},
  {0, sw7, 'H'}};
//--------------------------------------------------------
void setup() {
  for (int i=0; i<nSwitches; ++i)
    pinMode(buttons[i].swiNum, INPUT_PULLUP);
Keyboard.begin();
}
//--------------------------------------------------------
byte readSwitch (byte swiNum) {
  // Following inverts the pin reading (assumes pulldown = pressed)
  return 1 - digitalRead(swiNum);
}
//--------------------------------------------------------
void doAction(byte swin, char code, char action) {
  Keyboard.write(action);
}
//--------------------------------------------------------
void doButton(byte bn) {
  struct ButtonStruct *b = buttons + bn;
  if (b->bounceEnd) { // Was button changing?
    // It was changing, see if debounce time is done.
      if (b->bounceEnd < millis()) {
    b->bounceEnd = 0;    // Debounce time is done, so clear flag
    // See if the change was real, or a glitch
    if (readSwitch(b->swiNum) == b->swiPrev) {
      // Current reading is same as trigger reading, so do it
      if (b->swiPrev) {
        doAction(b->swiNum, 'P', b->swiActP);
      } else {
        doAction(b->swiNum, 'R', b->swiActR);
      }
    }
      }
  } else {  // It wasn't changing; but see if it's changing now
    if (b->swiPrev != readSwitch(b->swiNum)) {
      b->swiPrev = readSwitch(b->swiNum);
      b->bounceEnd = millis()+bounceMillis; // Set the Debounce flag
    }
  }
}
//--------------------------------------------------------
long int seconds, prevSec=0;
void loop() {
  for (int i=0; i<nSwitches; ++i)
    doButton(i);
}

重述问题: 我目前有这个代码可以去除一组简单的按钮,所以按下它们只需发送键控器(分别为a,b,c等)

我有两个问题

•我看到了这种设置按钮输出的方法,这对我正在编写的第三方程序配置按钮非常有效。问题是我不知道如何使用当前的方式发送键修饰符(如添加ctrl到'z'以执行撤消命令)。我知道arduino修饰符是KEY_LEFT_CTRL。假设我想要更改下面的按钮,提供“A”代替ctrl + z。我希望将{0,sw0,'A'}替换为像{0,sw0,KEY_LEFT_CTRL +'z'}(显然不起作用)这样的事情来实现这一点,以保持这种数组格式的顺畅便于配置。这可能吗?我找了一种注入十六进制的方法,就像0x80 + z一样,但这也是不行的。

•当前方法每次按下一次按键,但即使按住按钮也会停止输出。我想把它改成像键盘一样(按下并按住一次按键,等待一秒钟,然后开始重复按键,直到你松开。)我不知道从哪里开始。

1 个答案:

答案 0 :(得分:0)

在官方Arduino教程网站中,您有一个与ctrl LINK

组合的示例
#include "Keyboard.h"

// use this option for OSX.
// Comment it out if using Windows or Linux:
char ctrlKey = KEY_LEFT_GUI;
// use this option for Windows and Linux.
// leave commented out if using OSX:
//  char ctrlKey = KEY_LEFT_CTRL;

/*
 *SETUP CODE
 */

Keyboard.press(ctrlKey);
Keyboard.press('n');
delay(100);
Keyboard.releaseAll();