我目前正在尝试为我正在构建的键盘编写代码,但我在一个特定的问题上遇到了很多麻烦。我需要为键盘写一个数组,但是它并不像我期望的那样工作。当我在Keyboard.press函数中使用它们时,修饰符键起作用,但由于某种原因,当我在Keyboard.press
函数中从数组中调用它们时,它们就不起作用了。例如,如果我使用Keyboard.press(KEY_SPACE)
,它工作得很好,但由于某些原因,即使Keyboard.press(keys[0][0])
= KEY_SPACE,它也会输出。问题是J和K运行得很好,特别是那些格式为KEY_的文件运行得不太好
我使用的是Nicohood的HID项目库。当我使用标准的arduino键盘库时,它工作得很好,只是我需要使用HID项目库。
#include <HID-Project.h>
#include <HID-Settings.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp;
byte inputs[] = {4,5,6,7,8,9};
const int inCount = sizeof(inputs)/sizeof(inputs[0]);
byte outputs[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
const int outCount = sizeof(outputs)/sizeof(outputs[0]);
char keys[2][2] = {
{KEY_SPACE,'j'},
{'k',KEY_LEFT_SHIFT}
};
bool keysDown[2][2] = {
{false, false},
{false, false}
};
void setup() {
// put your setup code here, to run once:
mcp.begin();
for(int i=0; i<outCount; i++){ //declaring all the outputs and setting them high
mcp.pinMode(outputs[i],OUTPUT);
mcp.digitalWrite(outputs[i],LOW);
}
for(int i=0; i<inCount; i++){ //declaring all the inputs and activating the internal pullup resistor
pinMode(inputs[i],INPUT_PULLUP);
}
Serial.begin(9600);
Keyboard.begin();
}
void loop() {
// put your main code here, to run repeatedly:
keyCheck();
}
void keyCheck()
{
for (int i=0; i<2; i++){
mcp.digitalWrite(outputs[i],LOW);
for (int j=0; j<2; j++)
{
if(digitalRead(inputs[j]) == LOW && keysDown[i][j] == false)
{
Serial.print("Row: ");
Serial.print(i);
Serial.println();
Serial.print("Col: ");
Serial.print(j);
Serial.println();
Serial.print(keys[i][j]);
Serial.println();
Keyboard.press(keys[i][j]);
Serial.println();
keysDown[i][j] = true;
Serial.print("KeysDown set to true");
Serial.println();
}else if(digitalRead(inputs[j]) == HIGH && keysDown[i][j] == true)
{
Serial.print("keysdown set to false");
Serial.println();
Keyboard.release(keys[i][j]);
keysDown[i][j] = false;
}
delay(1);
}
mcp.digitalWrite(outputs[i], HIGH);
}
}
任何帮助都是非常感谢的。我已经尝试了一个多月来解决这个问题,这真的很令人沮丧,因为其他所有的事情都已经完成了,只是这个特定的问题阻碍了整个项目。耽误您时间,实在对不起。
发布于 2021-02-21 09:58:35
我怀疑您可能被您的调试输出格式欺骗了。
KEY_SPACE常量的值为十进制44 (十六进制2C),这是逗号的ASCII码。您通过Serial类将此值写入到您的终端,因此您会看到一个逗号。然后通过Keyboard类再次发送它,该类负责显示(不可见的)空间。
我用来避免这种混乱的一种约定是,总是在调试输出中使用开始/结束标记将变量内容括起来-例如,printf("row <%d> col <%d> char <%s>", i, j, key[i, j])
……或者类似的。
https://stackoverflow.com/questions/66286959
复制相似问题