【问题标题】:Arduino how to send a ctrl+z modifier with KEY_LEFT_CTRLArduino 如何使用 KEY_LEFT_CTRL 发送 ctrl+z 修饰符
【发布时间】:2015-11-12 05:38:03
【问题描述】:

我试图在 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 等)

我有两个问题

• 我看到了这种设置按钮输出的方法,该方法非常适用于我正在编写的用于配置按钮的第 3 方程序。问题是我不知道如何以当前的方式发送键修饰符(例如将 ctrl 添加到“z”以执行撤消命令)。我知道 arduino 修饰符是 KEY_LEFT_CTRL。假设我想将下方提供“A”的按钮更改为 ctrl+z。我希望将 {0, sw0, 'A'} 换成 {0, sw0, KEY_LEFT_CTRL + 'z'} 之类的东西(这显然不起作用)以使这种数组格式保持完整便于配置。这可能吗?我寻找了一种注入十六进制的方法,甚至像 0x80+z 一样,但这也是不行的。

• 当前方法每按一次按键,但即使您按住按钮也会停止输出。我想将其更改为像键盘一样(按住可提供一次击键,等待一秒钟,然后开始重复击键,直到您松开为止。)我不知道从哪里开始。

【问题讨论】:

  • 第二行肯定不是 enum 的用途。

标签: arduino usb keyboard-events ctrl


【解决方案1】:

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

【讨论】:

  • 官方教程看过了,但是不明白怎么把ctrl-z命令插入到上面的数组范围内。 {0, sw0, should go here} 我不能在该数组中放置两个keyboard.press 方法来实现一次击键
  • 这个结构是你设计的吗?我的意思是,如果你这样做了,你就会明白想要用它做什么......没有更多代码我什么也说不出来
  • 我看到了这种方法,所以我有点迷路了。我已经更新了完整的代码,并在新代码编辑下更好地表达了问题。
猜你喜欢
  • 2017-10-31
  • 2011-03-03
  • 2011-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多