Arduino - Keyboard Serial
Arduino - Emulating a USB Keyboard
Section titled “Arduino - Emulating a USB Keyboard”Certain Arduino boards with native USB capabilities can act as a Human Interface Device (HID), such as a keyboard or mouse. This allows your Arduino to type, press key combinations, and control a computer directly over USB. This feature is supported by boards like the Leonardo, Micro, Due, and Zero.
Boards like the Arduino Uno or Nano cannot use this feature directly, as their USB communication is handled by a separate chip.
!! CRITICAL WARNING !!
Section titled “!! CRITICAL WARNING !!”When you use the Keyboard library, your Arduino takes control of your computer’s keyboard. A buggy sketch can make it impossible to use your computer or upload new code. Always include a safety mechanism in your sketch, such as a physical switch or a startup delay, to prevent losing control.
Components Required
Section titled “Components Required”- 1 × Arduino Leonardo, Micro, Due, or other HID-compatible board
- 1 × USB Cable
No external wiring is needed. Just connect your board to the computer via USB.
The Keyboard.h Library
Section titled “The Keyboard.h Library”The Keyboard.h library is built-in for compatible boards and does not require installation. You just need to include it in your sketch.
Keyboard.begin(): Initializes the keyboard emulation.Keyboard.write(char): Presses and releases a single key. Can send printable ASCII characters or special key codes (e.g.,KEY_RETURN,KEY_LEFT_ARROW).Keyboard.print(string): Types out a sequence of characters.Keyboard.println(string): Types a string followed by a carriage return and newline.Keyboard.press(key): Holds down a key (e.g.,KEY_LEFT_CTRL).Keyboard.release(key): Releases a previously pressed key.Keyboard.releaseAll(): Releases all currently pressed keys.
Example 1: Simple Serial-to-Keystroke
Section titled “Example 1: Simple Serial-to-Keystroke”This sketch listens for a character on the serial monitor and types the character that follows it (e.g., sending a types b).
#include "Keyboard.h"
void setup() { // Safety delay: gives you 5 seconds to upload a new sketch // before the keyboard functionality starts. delay(5000);
Serial.begin(9600); Keyboard.begin(); // Start keyboard emulation}
void loop() { if (Serial.available() > 0) { char inChar = Serial.read();
// Don't type control characters unless intended if (isprint(inChar)) { Serial.print("Received: "); Serial.print(inChar); Serial.print(", Typing: "); Serial.println((char)(inChar + 1));
// Type the next ASCII value from the one received Keyboard.write(inChar + 1); } }}Example 2: A Simple Macro Pad
Section titled “Example 2: A Simple Macro Pad”This more advanced sketch demonstrates how to send commands and shortcuts, like opening Notepad on Windows and typing a message.
#include "Keyboard.h"
void setup() { delay(5000); // Safety delay! Keyboard.begin();
// Example: Run this once at startup openNotepadAndType();}
void loop() { // The loop is empty because the action runs only once in setup. // You could add buttons to trigger actions here.}
// A function to open Notepad (Windows) and type a messagevoid openNotepadAndType() { // Press Windows Key Keyboard.press(KEY_LEFT_GUI); delay(100); Keyboard.release(KEY_LEFT_GUI); delay(500);
// Type "notepad" and press Enter Keyboard.println("notepad"); delay(1000); // Wait for notepad to open
// Type a message Keyboard.println("Hello from your Arduino!"); Keyboard.println("This is a demonstration of the Keyboard library.");
// Example of a shortcut: Ctrl + S to save Keyboard.press(KEY_LEFT_CTRL); Keyboard.press('s'); delay(100); Keyboard.releaseAll();}Practical Applications and Common Pitfalls
Section titled “Practical Applications and Common Pitfalls”- Applications: Custom macro pads for shortcuts in software or gaming, assistive technology devices, automating repetitive data entry.
- How to Recover: If a sketch takes over your PC, you need to time the upload of a blank sketch. Press and hold the reset button on your Arduino, click Upload in the IDE, and release the reset button as soon as the IDE says “Uploading…”.
- Keyboard Layout: The
Keyboardlibrary defaults to a US-English layout. Using it on a computer with a different layout may produce incorrect characters. Custom libraries are needed for full international support.