Arduino: Key Press Simulation (Without Button)

A USB Keyboard function in Arduino can be used to send keystrokes to an attached computer.

Unfortunately this capability is limited to Arduino boards with the ATmega32u4 microchip i.e. Arduino Leonardo, Arduino Micro and Arduino-compatible Pro Micro (though the last one is really cheap).

Below you will find some code snippets with the examples of how to simulate keystrokes, including a multiple key pressing, using the Arduino boards.

To make it more simple, the keystrokes in the examples below will be called in a loop, so there is no need to install any additional hardware buttons.

Cool Tip: How to capture a keystroke and detect the pressed key in Windows PowerShell! Read more →

Key Press Simulation using Arduino

A simple example of a keyboard input simulation using Arduino:

#include <Keyboard.h>

void setup() {
  Keyboard.begin();
}

void loop() {
  Keyboard.write('H');
  Keyboard.write('e');
  Keyboard.write('y');
  Keyboard.write('!');
  Keyboard.write(' ');
  delay(5000); // 5sec
}

Cool Tip: Generate random numbers and random delays in Arduino! Read more →

Use the following code snippet to simulate a multiple key press:

#include <Keyboard.h>

void setup() {
  Keyboard.begin();
}

void loop() {
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_ALT);
  Keyboard.press(KEY_DELETE);
  delay(100);
  Keyboard.releaseAll();
  delay(60000); // 1min
}

Simulate a press of the F15 key each 10 minutes, to prevent a policy-enforced screen lock after some period of inactivity in Windows:

#include <Keyboard.h>

void setup() {
  Keyboard.begin();
}

void loop() {
  Keyboard.press(KEY_F15);
  delay(100);
  Keyboard.release(KEY_F15);
  delay(60000); // 10min
}

The list of the all supported special keys can be found here.

Cool Tip: Prevent a screen lock when idle in Windows using Powershell! Read more →

Leave a Reply