Hi @johnwasser and @Matis7507,
I found this
The 6 bytes (after the first two bytes) represent the keys that are concurrently pressed (up to 6 in this case). If no keys are currently pressed then all 6 bytes should contain 0x00. Each byte is an index between LOGICAL_MINIMUM and LOGICAL_MAXIMUM that is mapped one-for-one to a range of keyboard usages that are (usually) defined by USAGE_MINIMUM and USAGE_MAXIMUM, or a list of individual USAGEs, or a combination of both. If, for example, the A and B keys are pressed then the buffer will contain the index for those two usages (in any order) and the remaining bytes should be 0x00. If A is subsequently released then the buffer should still contain the index for the B usage (with the remaining bytes set to 0x00). If you forget to signal key releases by clearing the buffer to zeros then you will most likely see the last changed key being repeated on your host device (usually a PC). The key repeat delay and rate is purely a host function.
Source:
https://stackoverflow.com/questions/24055687/hid-usb-keyboard
So the easiest solution for 4 keys would be to reserve a separate place for each single key in the 6 byte keyboard buffer like this (which I tested in debug mode):
uint8_t buf[8] = {
0
}; // Keyboard Report Buffer: 8 bytes
#define PIN_W A0
#define PIN_A A1
#define PIN_S A2
#define PIN_D A3
//#define SERIAL_DEBUG // for serial debug: remove //
// for actual HID: put //
bool currState_W = HIGH;
bool currState_A = HIGH;
bool currState_S = HIGH;
bool currState_D = HIGH;
bool prevState_W = HIGH;
bool prevState_A = HIGH;
bool prevState_S = HIGH;
bool prevState_D = HIGH;
unsigned long prevTime_W = 0;
unsigned long prevTime_A = 0;
unsigned long prevTime_S = 0;
unsigned long prevTime_D = 0;
unsigned long waitTime_W = 50;
unsigned long waitTime_A = 50;
unsigned long waitTime_S = 50;
unsigned long waitTime_D = 50;
void setup()
{
Serial.begin(9600);
pinMode(PIN_W, INPUT_PULLUP);
pinMode(PIN_A, INPUT_PULLUP);
pinMode(PIN_S, INPUT_PULLUP);
pinMode(PIN_D, INPUT_PULLUP);
delay(200);
}
void loop()
{
checkButton();
}
void checkButton() {
bool currRead_W = digitalRead(PIN_W);
bool currRead_A = digitalRead(PIN_A);
bool currRead_S = digitalRead(PIN_S);
bool currRead_D = digitalRead(PIN_D);
if (currRead_W != prevState_W) {
prevTime_W = millis();
}
if (currRead_A != prevState_A) {
prevTime_A = millis();
}
if (currRead_S != prevState_S) {
prevTime_S = millis();
}
if (currRead_D != prevState_D) {
prevTime_D = millis();
}
if ((millis() - prevTime_W) > waitTime_W) {
if (currRead_W != currState_W) {
currState_W = currRead_W;
if (currState_W == LOW) {
// Send the code
buf[2] = 26; // HID: W key
#ifdef SERIAL_DEBUG
buf[2] = 'W'; // Serial: W key
#endif
Serial.write(buf, 8); // Send keypress
} else {
// Release the keyboard
releaseKey(2);
}
}
}
if ((millis() - prevTime_A) > waitTime_A) {
if (currRead_A != currState_A) {
currState_A = currRead_A;
if (currState_A == LOW) {
// Send the code
buf[3] = 4; // HID: A key
#ifdef SERIAL_DEBUG
buf[3] = 'A'; // Serial: A key
#endif
Serial.write(buf, 8); // Send keypress
} else {
// Release the keyboard
releaseKey(3);
}
}
}
if ((millis() - prevTime_S) > waitTime_S) {
if (currRead_S != currState_S) {
currState_S = currRead_S;
if (currState_S == LOW) {
// Send the code
buf[4] = 22; // HID: S key
#ifdef SERIAL_DEBUG
buf[4] = 'S'; // Serial: S key
#endif
Serial.write(buf, 8); // Send keypress
} else {
// Release the keyboard
releaseKey(4);
}
}
}
if ((millis() - prevTime_D) > waitTime_D) {
if (currRead_D != currState_D) {
currState_D = currRead_D;
if (currState_D == LOW) {
// Send the code
buf[5] = 7; // HID: D key
#ifdef SERIAL_DEBUG
buf[5] = 'D'; // Serial: D key
#endif
Serial.write(buf, 8); // Send keypress
} else {
// Release the keyboard
releaseKey(5);
}
}
}
prevState_W = currRead_W;
prevState_A = currRead_A;
prevState_S = currRead_S;
prevState_D = currRead_D;
}
void releaseKey(byte index)
{
buf[0] = 0;
buf[index] = 0;
Serial.write(buf, 8); // Release key
}
As the sketch uses lots of repetitions I have "cleaned up" a little bit by using a structure and for-loops (also tested in the debug version):
uint8_t buf[8] = {0}; // Keyboard Report Buffer: 8 bytes
#define PIN_W A0
#define PIN_A A1
#define PIN_S A2
#define PIN_D A3
//#define SERIAL_DEBUG // for serial debug: remove //
// for actual HID: put //
struct keyType {
char Key;
byte Pin;
byte HiD;
byte currState = HIGH;
byte prevState = HIGH;
unsigned long prevTime = 0;
unsigned long waitTime = 50;
};
const int NoOfKeys = 4;
keyType k[NoOfKeys];
void setup()
{
Serial.begin(9600);
k[0].Key = 'W';
k[0].Pin = A0;
k[0].HiD = 26;
k[1].Key = 'A';
k[1].Pin = A1;
k[1].HiD = 4;
k[2].Key = 'S';
k[2].Pin = A2;
k[2].HiD = 22;
k[3].Key = 'D';
k[3].Pin = A3;
k[3].HiD = 7;
for (int i = 0; i < NoOfKeys; i++)
pinMode(k[i].Pin, INPUT_PULLUP);
delay(200);
}
void loop()
{
checkButton();
}
void checkButton() {
for (int i = 0; i < NoOfKeys;i++) {
byte currRead = digitalRead(k[i].Pin);
if (currRead != k[i].prevState) k[i].prevTime = millis();
if ((millis() - k[i].prevTime) > k[i].waitTime) {
if (currRead != k[i].currState) {
k[i].currState = currRead;
if (k[i].currState == LOW) {
// Send the code
buf[2+i] = k[i].HiD; // HID: key
#ifdef SERIAL_DEBUG
buf[2+i] = k[i].Key; // Serial: key
#endif
Serial.write(buf, 8); // Send keypress
} else {
// Release the keyboard
buf[0] = 0;
buf[2+i] = 0;
Serial.write(buf, 8); // Release key
}
}
}
k[i].prevState = currRead;
}
}
Both sketches should do it ... (unless the HID function does ignore data after recognizing a 0 character. In that case the software had to fill the array always from "left to right" which would need some changes to the software).
@Matis7507, could give it a try?
[Edit]: The following sketch is moving the keys "to the left" leaving no 0s in between:
uint8_t buf[8] = {0}; // Keyboard Report Buffer: 8 bytes
#define PIN_W A0
#define PIN_A A1
#define PIN_S A2
#define PIN_D A3
//#define SERIAL_DEBUG // for serial debug: remove //
// for actual HID: put //
struct keyType {
char Key;
byte Pin;
byte HiD;
byte currState = HIGH;
byte prevState = HIGH;
unsigned long prevTime = 0;
unsigned long waitTime = 50;
};
const int NoOfKeys = 4;
keyType k[NoOfKeys];
void setup()
{
Serial.begin(9600);
k[0].Key = 'W';
k[0].Pin = A0;
k[0].HiD = 26;
k[1].Key = 'A';
k[1].Pin = A1;
k[1].HiD = 4;
k[2].Key = 'S';
k[2].Pin = A2;
k[2].HiD = 22;
k[3].Key = 'D';
k[3].Pin = A3;
k[3].HiD = 7;
for (int i = 0; i < NoOfKeys; i++)
pinMode(k[i].Pin, INPUT_PULLUP);
delay(200);
}
void loop()
{
checkButton();
}
void SendBuf(){
for (byte i=2;i<6;i++){
#ifdef SERIAL_DEBUG
if (buf[i] == '-')
#else
if (buf[i] == 0)
#endif
for (byte j=0;j<6-i;j++)
buf[i+j] = buf[i+j+1];
}
Serial.write(buf, 8); // Send keypress
}
void checkButton() {
for (int i = 0; i < NoOfKeys;i++) {
byte currRead = digitalRead(k[i].Pin);
if (currRead != k[i].prevState) k[i].prevTime = millis();
if ((millis() - k[i].prevTime) > k[i].waitTime) {
if (currRead != k[i].currState) {
k[i].currState = currRead;
if (k[i].currState == LOW) {
// Send the code
#ifdef SERIAL_DEBUG
buf[2+i] = k[i].Key; // Serial: key
#else
buf[2+i] = k[i].HiD; // HID: key
#endif
SendBuf();
} else {
// Release the keyboard
#ifdef SERIAL_DEBUG
buf[2+i] = '-';
#else
buf[0] = 0;
buf[2+i] = 0;
#endif
SendBuf();
}
}
}
k[i].prevState = currRead;
}
}