Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

Arduino keyboard array

I'm trying to build my own keyboard using an arduino pro micro, but I'm having some trouble with the code. I'm trying to set up a 2 dimensional array for all my keys, and most of them work fine. The problem is the modifier keys. For keys like the space key, when I call them as Keyboard.press(KEY_SPACE) it's fine, but since I'm using an array in the code it's called as Keyboard.press(layout[i][j]), and when it points to KEY_SPACE in the array, it outputs , for some reason. There's a similar problem with all the keys, like shift. With shift when I put Keyboard.press(KEY_LEFT_SHIFT) in the setup section and then press a key it capitalizes that key normally, but when the layout calls KEY_LEFT_SHIFT from an array, it doesn't do that.

Here's the code:

#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_LEFT_SHIFT, 'j'},
  {'k','w'}
};

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

I'm using an arduino pro micro, and the library I'm using for the keyboard is NicoHoods HID library

Sorry, I'm not great at explaining things, so let me know if you need any clarification or have any questions or anything.

Thanks

question from:https://stackoverflow.com/questions/65837385/arduino-keyboard-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...