Use an Arduino board to emulate a keyboard

In this article I will show how to use an Arduino board to build a very basic USB keyboard and how to control a windows computer.

The functions to emulate a basic keyboard are already implemented in the core libraries shipped with the Arduino IDE. These libraries can be used to turn a 32u4 based board or an Arduino Due or Zero board into a keyboard or mouse. At the moment it is not possible to use these libraries with other Arduino boards like the popular Arduino Uno.

To get started we will use the small example below. After connecting the Arduino to an windows computer it will wait a few seconds, press some keys to open the notepad application and write Hello World into the text box.

In detail the following actions have to be done.

  • Wait 10 seconds to give Windows some time to initialize the keyboard
  • Press Windows + r to open the run dialog
  • Wait 0.1 seconds
  • Release all keys
  • Wait 0.5 seconds
  • Write notepad.exe into the input field
  • Press Return key
  • Wait 0.1 seconds
  • Release all keys to execute the command
  • Wait 2 seconds to let windows start the notepad application
  • Write Hello World into the text box

The final source should look like the example below.

#include "Keyboard.h"

void setup() {
  Keyboard.begin();
  delay(10000);
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('r');
  delay(100);
  Keyboard.releaseAll();
  delay(500);
  Keyboard.print("notepad.exe");
  Keyboard.press(KEY_RETURN);
  delay(100);
  Keyboard.releaseAll();
  delay(2000);
  Keyboard.print("Hello World");
}

void loop() {}

Links

Verwandte Artikel