Skip to content

Instantly share code, notes, and snippets.

@mojoaxel
Created December 29, 2025 13:33
Show Gist options
  • Select an option

  • Save mojoaxel/bfb0a610ac28fcd0799dca0c2d44816d to your computer and use it in GitHub Desktop.

Select an option

Save mojoaxel/bfb0a610ac28fcd0799dca0c2d44816d to your computer and use it in GitHub Desktop.
Send a space character if a button is pressed
/*
* For the Arduino Leonardo and Micro.
* Sends a space character when a button is pressed.
*
* This based on an official example code is in the public domain.
* https://docs.arduino.cc/built-in-examples/usb/KeyboardMessage/
*/
#include "Keyboard.h"
const int buttonPin = 4; // input pin for pushbutton
int previousButtonState = HIGH; // for checking the state of a pushButton
int counter = 0; // button push counter
void setup() {
// make the pushButton pin an input:
pinMode(buttonPin, INPUT);
// initialize control over the keyboard:
Keyboard.begin();
}
void loop() {
int buttonState = digitalRead(buttonPin);
if ((buttonState == HIGH) && (buttonState != previousButtonState)) {
// send a "space" character (once)
Keyboard.print(" ");
// only send a character every 0.5s (max speed)
delay(500);
}
previousButtonState = buttonState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment