Hallo together,
I built a keyboard myself. I got the code from the Internet and reworked it a bit because I still connected a display and LEDs. The keyboard works well when typing. But not with video games if you want to run with it (WASD). Is it up to the library, the command Keyboard.press (); or only the game?
(I know the code is a bit cumbersome)
Any support would be great. Many Thanks
(I deleted the code for the leds and the display because it would then be confusing)
#include <Keyboard.h>
int wort;
int esc = 0;
int zuruck = 0;
int tab = 0;
int capslock = 0;
int enter = 0;
int shiftlinks = 0;
int shiftrechts = 0;
int ctrllinks = 0;
int windowslinks = 0;
int altlinks = 0;
int leerzeichen = 0;
int altrechts = 0;
int windowsrechts = 0;
int ctrlrechts = 0;
int longPressDelay = 350;
int spamSpeed = 15;
byte inputs[] = {22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7};
#define inCount sizeof(inputs)/sizeof(inputs[0])
byte outputs[] = {6, 5, 4, 38, 39};
#define outCount sizeof(outputs)/sizeof(outputs[0])
char layout[5][15] = {
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45},
{46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60},
{61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75},
};
int keyDown[outCount][inCount];
bool keyLong[outCount][inCount];
void setup() {
for (int i = 0; i < outCount; i++) {
pinMode(outputs[i], OUTPUT);
digitalWrite(outputs[i], HIGH);
}
for (int i = 0; i < inCount; i++) {
pinMode(inputs[i], INPUT_PULLUP);
}
Serial.begin(9600);
Serial.println("verbunden");
Keyboard.begin();
}
void loop() {
for (int i = 0; i < outCount; i++)
{
digitalWrite(outputs[i], LOW);
delayMicroseconds(5);
for (int j = 0; j < inCount; j++)
{
if (digitalRead(inputs[j]) == LOW)
{
keyPressed(i, j);
}
else if (keyDown[i][j] != 0)
{
resetKey(i, j);
}
}
digitalWrite(outputs[i], HIGH);
delayMicroseconds(500);
}
}
void keyPressed(int row, int col) {
Serial.print("Output: ");
Serial.print(row);
Serial.print(" Input: ");
Serial.print(col);
Serial.print(" ");
Serial.println(layout[row][col]);
if (layout[row][col] == 2) { //1
Serial.println("1");
wort = 49;
}
if (layout[row][col] == 3) { //2
Serial.println("2");
wort = 50;
}
if (layout[row][col] == 4) { //3
Serial.println("3");
wort = 51;
}
if (layout[row][col] == 5) { //4
Serial.println("4");
wort = 52;
}
if (layout[row][col] == 6) { //5
Serial.println("5");
wort = 53;
}
if (layout[row][col] == 7) { //6
Serial.println("6");
wort = 54;
}
if (layout[row][col] == 8) { //7
Serial.println("7");
wort = 55;
}
if (layout[row][col] == 9) { //8
Serial.println("8");
wort = 56;
}
if (layout[row][col] == 10) { //9
Serial.println("9");
wort = 57;
}
if (layout[row][col] == 11) { //0
Serial.println("0");
wort = 48;
}
if (layout[row][col] == 12) { //ß
Serial.println("ß");
}
if (layout[row][col] == 13) { //^
Serial.println("^");
wort = 94;
}
if (layout[row][col] == 18) { //q
Serial.println("q");
wort = 113;
}
if (layout[row][col] == 19) { //w
Serial.println("w");
wort = 119;
}
if (layout[row][col] == 20) { //e
Serial.println("e");
wort = 101;
}
if (layout[row][col] == 21) { //r
Serial.println("r");
wort = 114;
}
if (layout[row][col] == 22) {
Serial.println("t"); //t
wort = 116;
}
if (layout[row][col] == 23) { //z
Serial.println("z");
wort = 122;
}
if (layout[row][col] == 24) { //u
Serial.println("u");
wort = 117;
}
if (layout[row][col] == 25) { //i
Serial.println("i");
wort = 105;
}
if (layout[row][col] == 26) { //o
Serial.println("o");
wort = 111;
}
if (layout[row][col] == 27) { //p
Serial.println("p");
wort = 112;
}
if (layout[row][col] == 28) { //ü
Serial.println("ü");
wort = 129;
altlinks = 10;
}
if (layout[row][col] == 29) { //+
Serial.println("+");
wort = 43;
}
if (layout[row][col] == 30) { //-
Serial.println("-");
wort = 45;
}
if (layout[row][col] == 33) { //a
Serial.println("a");
wort = 97;
}
if (layout[row][col] == 34) { //s
Serial.println("s");
wort = 115;
}
if (layout[row][col] == 35) { //d
Serial.println("d");
wort = 100;
}
if (layout[row][col] == 36) { //f
Serial.println("f");
wort = 102;
}
if (layout[row][col] == 37) { //g
Serial.println("g");
wort = 103;
}
if (layout[row][col] == 38) { //h
Serial.println("h");
wort = 104;
}
if (layout[row][col] == 39) { //j
Serial.println("j");
wort = 106;
}
if (layout[row][col] == 40) { //k
Serial.println("k");
wort = 107;
}
if (layout[row][col] == 41) { //l
Serial.println("l");
wort = 108;
}
if (layout[row][col] == 42) { //ö
Serial.println("ö");
wort = 148;
altlinks = 10;
}
if (layout[row][col] == 43) { //ä
Serial.println("ä");
wort = 132;
altlinks = 10;
}
if (layout[row][col] == 49) { //<
Serial.println("y");
wort = 121;
}
if (layout[row][col] == 48) { //<
Serial.println("<");
wort = 60;
}
if (layout[row][col] == 50) { //x
Serial.println("x");
wort = 120;
}
if (layout[row][col] == 51) { //c
Serial.println("c");
wort = 99;
}
if (layout[row][col] == 52) { //v
Serial.println("v");
wort = 118;
}
if (layout[row][col] == 53) { //b
Serial.println("b");
wort = 98;
}
if (layout[row][col] == 54) { //n
Serial.println("n");
wort = 110;
}
if (layout[row][col] == 55) { //m
Serial.println("m");
wort = 109;
}
if (layout[row][col] == 56) { //,
Serial.println(",");
wort = 44;
}
if (layout[row][col] == 57) { //.
Serial.println(".");
wort = 46;
}
//macros
if (row == 2 && col == 15) { //makro1: Ctrl+v
Serial.println("marko1");
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('v');
Keyboard.releaseAll();
delay(350);
}
if (row == 1 && col == 15) { //makro2: Ctrl+c
Serial.println("marko2");
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('c');
Keyboard.releaseAll();
delay(350);
}
if (row == 3 && col == 15) { //makro4: Windows+G (Xbox app)
Serial.println("makro4");
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('g');
Keyboard.releaseAll();
delay(500);
}
if (row == 4 && col == 15) { //makro3: Alt+y (Disord mute)
Serial.println("makro3");
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press('Y');
Keyboard.releaseAll();
delay(500);
}
//funktionen
if (row == 0 && col == 0) { //ESC
Serial.println("ESC");
esc = 10;
}
if (row == 0 && col == 14) { //zurück
Serial.println("zurück");
zuruck = 10;
}
if (row == 1 && col == 0) { //Tab
Serial.println("tab");
tab = 10;
}
if (row == 2 && col == 0) { //caps lock
Serial.println("caps lock");
capslock = 10;
}
if (row == 2 && col == 13) { //enter
Serial.println("Enter");
enter = 10;
}
if (row == 3 && col == 1) { //Shift links
shiftlinks = 10;
}
if (row == 3 && col == 13) { //Shift rechts
Serial.println("rechts shift");
shiftrechts = 10;
}
if (row == 4 && col == 0) { //CTRL links
Serial.println("ctrl links");
ctrllinks = 10;
}
if (row == 4 && col == 1) { //windows taste links
Serial.println("windows taste");
windowslinks = 10;
}
if (row == 4 && col == 3) { //alt links
Serial.println("alt links");
altlinks = 10;
}
if (row == 4 && col == 6) { //space
Serial.println("leerzeichen");
leerzeichen = 10;
}
if (row == 4 && col == 10) { //alt rechts
Serial.println("rechts alt");
altrechts = 10;
}
if (row == 4 && col == 11) { //windows taste rechts
Serial.println("windows taste");
windowsrechts = 10;
}
if (row == 4 && col == 14) { //ctrl rechts
Serial.println("ctrl links");
ctrlrechts = 10;
}
if (keyDown[row][col] == 0) {
if (esc == 10) {
Keyboard.press(KEY_ESC);
esc = 0;
delay(50);
}
if (zuruck == 10) {
Keyboard.press(KEY_BACKSPACE);
zuruck = 0;
delay(50);
}
if (tab == 10) {
Keyboard.press(KEY_TAB);
tab = 0;
delay(50);
}
if (capslock == 10) {
Keyboard.press(KEY_CAPS_LOCK);
capslock = 0;
delay(50);
}
if (enter == 10) {
Keyboard.press(KEY_ENTER);
enter = 0;
delay(50);
}
if (shiftlinks == 10) {
Keyboard.press(KEY_LEFT_SHIFT);
shiftlinks = 0;
delay(50);
}
if (shiftrechts == 10) {
Keyboard.press(KEY_RIGHT_SHIFT);
shiftrechts = 0;
delay(50);
}
if (ctrllinks == 10) {
Keyboard.press(KEY_LEFT_CTRL);
ctrllinks = 0;
delay(50);
}
if (windowslinks == 10) {
Keyboard.press(KEY_LEFT_GUI);
windowslinks = 0;
delay(50);
}
if (altlinks == 10) {
Keyboard.press(KEY_LEFT_ALT);
altlinks = 0;
delay(50);
}
if (leerzeichen == 10) {
Keyboard.press(32);
leerzeichen = 0;
delay(50);
}
if (altrechts == 10) {
Keyboard.press(KEY_RIGHT_ALT);
altrechts = 0;
delay(50);
}
if (windowsrechts == 10) {
Keyboard.press(KEY_RIGHT_GUI);
windowsrechts = 0;
delay(50);
}
if (ctrlrechts == 10) {
Keyboard.press(KEY_LEFT_CTRL);
ctrlrechts = 0;
delay(50);
}
Keyboard.press(wort);
Serial.println(wort);
Keyboard.releaseAll();
}
else if (keyLong[row][col] && keyDown[row][col] > spamSpeed) {
if (esc == 10) {
Keyboard.press(KEY_ESC);
esc = 0;
}
if (zuruck == 10) {
Keyboard.press(KEY_BACKSPACE);
zuruck = 0;
}
if (tab == 10) {
Keyboard.press(KEY_TAB);
tab = 0;
}
if (capslock == 10) {
Keyboard.press(KEY_CAPS_LOCK);
capslock = 0;
}
if (enter == 10) {
Keyboard.press(KEY_ENTER);
enter = 0;
}
if (shiftlinks == 10) {
Keyboard.press(KEY_LEFT_SHIFT);
shiftlinks = 0;
}
if (shiftrechts == 10) {
Keyboard.press(KEY_RIGHT_SHIFT);
shiftrechts = 0;
}
if (ctrllinks == 10) {
Keyboard.press(KEY_LEFT_CTRL);
ctrllinks = 0;
}
if (windowslinks == 10) {
Keyboard.press(KEY_LEFT_GUI);
windowslinks = 0;
}
if (altlinks == 10) {
Keyboard.press(KEY_LEFT_ALT);
altlinks = 0;
}
if (leerzeichen == 10) {
Keyboard.press(32);
leerzeichen = 0;
}
if (altrechts == 10) {
Keyboard.press(KEY_RIGHT_ALT);
altrechts = 0;
}
if (windowsrechts == 10) {
Keyboard.press(KEY_RIGHT_GUI);
windowsrechts = 0;
}
if (ctrlrechts == 10) {
Keyboard.press(KEY_LEFT_CTRL);
ctrlrechts = 0;
}
Keyboard.press(wort);
Keyboard.releaseAll();
keyDown[row][col] = 1;
}
else if (keyDown[row][col] > longPressDelay) {
keyLong[row][col] = true;
}
keyDown[row][col]++;
}
void resetKey(int row, int col) {
keyDown[row][col] = 0;
keyLong[row][col] = false;
wort = 0;
esc = 0;
zuruck = 0;
tab = 0;
capslock = 0;
enter = 0;
shiftlinks = 0;
shiftrechts = 0;
ctrllinks = 0;
windowslinks = 0;
altlinks = 0;
leerzeichen = 0;
altrechts = 0;
windowsrechts = 0;
ctrllinks = 0;
}