【发布时间】: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