After looking at many tutorials and trying many approaches with different Arduinos (Uno, because it's easyer to prototype) (and taking a short brake so I can try again from 0 with a new mindset) I made it work.
Before, I could do it because I was using Microcontrollers that had a 3/5V output also (ex. Uno, Mega, Nano, STM32, ESP8266, ESP32), so I could do a pull-up/down circuit, but because the Arduino Micro doesn't have such pins (it has only RAW as output, which can fry the board if it's externally powered) I had to use this method where I only use a GND pin and a Digital Input without any ressistors wired to the button.
Thanks for the answers and suggestions!
The new code (without the LED functionality):
#include "Keyboard.h"
// defining button pin numbers
// TODO: rename variables according to functionality
#define BUTTON_10 10
#define BUTTON_16 16
#define BUTTON_14 14
#define BUTTON_9 9
// giving them a first-state of HIGH
int previousButtonState10 = HIGH;
int previousButtonState16 = HIGH;
int previousButtonState14 = HIGH;
int previousButtonState9 = HIGH;
void setup() {
// initializing the buttons as INPUTS with PULLUP (internal pull-up resistor from Arduino)
pinMode(BUTTON_10, INPUT_PULLUP);
pinMode(BUTTON_16, INPUT_PULLUP);
pinMode(BUTTON_14, INPUT_PULLUP);
pinMode(BUTTON_9, INPUT_PULLUP);
// only for test
// TODO: Move this only to the function needed when more functionality is added
Keyboard.begin();
}
void loop() {
// checking if any of the buttons were pressed and saving the state in a variable
previousButtonState10 = checkButton(BUTTON_10, previousButtonState10);
previousButtonState16 = checkButton(BUTTON_16, previousButtonState16);
previousButtonState14 = checkButton(BUTTON_14, previousButtonState14);
previousButtonState9 = checkButton(BUTTON_9, previousButtonState9);
}
// function: reads the button state and compares it with the previous state to find out if it was pressed or not
// params: buttonPin => the pin number of the button; previousButtonState => the state corresponding for each button
// return: it returns the red state, so it can be saved as previousState in the coresponding parameter
int checkButton(int buttonPin, int previousButtonState) {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW && previousButtonState == HIGH) {
Keyboard.println(buttonPin);
// some minimal debouncing
// TODO: WIll be a problem when using millis() in the future
delay(50);
}
if (buttonState == HIGH && previousButtonState == LOW) {
// not needed, but some functionality could be added in the future
// Keyboard.println("release");
delay(50);
}
return buttonState;
}
Now I can work further on improving and extending the code to make the "Macro Board" project with mouse movement (and I also have to solder the buttons and LEDs to the board :P )