Hello,
the Examples shows how to send characters or send key pushes, but I can't figure out, how to do get a pushbutton connected to a Leonardo, acting as a real Keyboard Key will do, like the Button "w" for typing and gaming with the same sketch.
Is it just a question of delays between and after Keyboard.press('w'); + Keyboard.release('w'); ?
Or an if-while-release function without delays like:
void loop() {
if (digitalRead(7) == HIGH) {
while (digitalRead(7) == HIGH) {
Keyboard.press('w');
}
Keyboard.release('w');
}//if
}//loop
Or do I need to use an Interrupt to detect RISING and FALLING of the pushbutton, like:
Pressing a single button is done with Keyboard.write(), or Keyboard.print().
Keyboard.write('w');
When you want more control, or more keys pressed at the same time, then you can use the Keyboard.press() and Keyboard.release(). The computer will take care of the autorepeat while the button is pressed.
Call each of them just once. As soon as Keyboard.press() is called, the computer thinks that the button is kept pressing down. The button is released with Keyboard.release().
Often a delay of 100ms between them is okay.
Calling Keyboard.press() over and over again while a button is pressed it not okay.
When you want to transfer a mechanical button to the Keyboard object, then detect when the button is pressed and call Keyboard.press() and detect when the button is released for Keyboard.release(). There is no need to use interrupts.
Detecting a change of the button is in this example : https://www.arduino.cc/en/Tutorial/StateChangeDetection
Although I don't like that example, it show that the previous state of the button has to be remembered to detect a change.