[英]Arduino as Keyboard HID: Program runs differently in Keyboard/Programming mode
[英]Arduino HID Project Keyboard Array
我目前正在嘗試為我正在構建的鍵盤編寫代碼,但我在一個特定問題上遇到了很多麻煩。 我需要為他們的鍵盤編寫一個數組,但它並沒有像我期望的那樣工作。 當我在 Keyboard.press function 中使用修飾鍵時,修飾鍵起作用,但當我從Keyboard.press
function 中的數組調用它們時不起作用。 例如,如果我做Keyboard.press(KEY_SPACE)
它工作正常,但Keyboard.press(keys[0][0])
輸出,出於某種原因,即使 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);
}
}
任何幫助是極大的贊賞。 一個多月以來,我一直在努力解決這個問題,當其他所有事情都完成時,這真的很令人沮喪,只是這個特定問題一直在阻礙整個項目。 謝謝你的時間。
我懷疑你可能被你的調試 output 格式欺騙了。
KEY_SPACE 常量的值是十進制 44(十六進制 2C),它是逗號的 ASCII 代碼點。 您正在通過串行 class 將此值寫入終端,因此您會看到一個逗號。 然后你通過鍵盤 class 再次發送它,它盡職盡責地顯示(不可見的)空間。
我用來避免這種混淆的約定是始終在調試 output 中使用開始/結束標記包含變量內容 - 例如, printf("row <%d> col <%d> char <%s>", i, j, key[i, j])
... 或類似的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.