| |

Mastering DigiSpark HID: A Comprehensive Guide to DigiKeyboard Commands

The DigiSpark is a tiny development board based on the ATtiny85 microcontroller. Despite its thumb‑drive size, it boasts built‑in USB, six general‑purpose I/O pins and 8 kB of flash memory. This makes it ideal for projects where space is limited or when you need a low‑cost alternative to full‑sized Arduino boards. One powerful use of DigiSpark is to emulate a USB keyboard – turning the board into a Human Interface Device (HID) that can send keystrokes to a host computer. In this guide you’ll learn how to set up DigiSpark as an HID keyboard, understand the DigiKeyboard library and explore a full list of key commands, code examples and project ideas.

What makes DigiSpark special?

DigiSpark’s appeal comes from its small size and versatility. The board uses the ATtiny85 microchip and plugs directly into your computer for programming and power. It can be powered via USB or an external source (5 V or 7–35 V) and has an on‑board 5 V regulator. You get six I/O pins (with two reserved for USB when HID is active) and built‑in USB for serial debugging. Because it uses the familiar Arduino IDE, DigiSpark is approachable even for beginners. These features make it perfect for creating custom keyboards, macro pads or automation tools.

Understanding HID and DigiKeyboard

A Human Interface Device (HID) is any gadget that helps people interact with computers – keyboards, mice, gamepads and more. HID devices are plug‑and‑play because operating systems understand the HID protocol without special drivers. DigiSpark’s ability to act as a USB keyboard comes from the DigiKeyboard library, which implements a HID report descriptor and exposes functions to send key strokes. When you upload a sketch using this library, the board identifies itself as a keyboard and can send keystrokes just like a real one.

Getting started: Setting up DigiSpark as a keyboard

To program DigiSpark you’ll need a board, a computer and (if your model requires it) a Micro USB cable. Start by installing the Arduino IDE. Then add the DigiSpark boards package: open File → Preferences, paste the URL http://digistump.com/package_digistump_index.json into “Additional Boards Manager URLs”, and use Tools → Board → Boards Manager to install the DigiSpark package. For HID projects it’s recommended to use Arduino IDE 1.6.7 to avoid upload errors.

Once the board definition is installed, open a new sketch and include the DigiKeyboard library. Here’s a simple “Hello, World!” example:

#include <DigiKeyboard.h>

void setup() {
  // initialization
}

void loop() {
  DigiKeyboard.sendKeyStroke(0);        // send a dummy keystroke to avoid missing the first character
  DigiKeyboard.print("Hello, World!");  // type a string
  DigiKeyboard.sendKeyStroke(KEY_ENTER); // press Enter
  DigiKeyboard.delay(5000);              // wait 5 seconds while keeping USB alive
}

Connect the DigiSpark only when the IDE prompts you during upload. After the sketch uploads, open a text editor and watch the board type “Hello, World!” by itself.

Understanding DigiKeyboard functions

  • DigiKeyboard.sendKeyStroke(key, modifiers) – sends a single keystroke. The first argument is the key code, and the optional second argument is a bitwise combination of modifier keys (Control, Shift, Alt, GUI). For example, DigiKeyboard.sendKeyStroke(KEY_ENTER, MOD_ALT_LEFT) presses Alt + Enter.
  • DigiKeyboard.print() and println() – print ASCII text. Useful for typing strings; these functions handle proper key codes internally.
  • DigiKeyboard.delay(ms) – delays while keeping the USB connection alive. Use this instead of delay() so that the host doesn’t think the keyboard disconnected.
  • sendKeyPress() – similar to sendKeyStroke() but allows you to press and release keys separately (for long presses).
  • Modifier constants – the library defines left and right modifiers such as MOD_CONTROL_LEFT, MOD_SHIFT_LEFT, MOD_ALT_LEFT and MOD_GUI_LEFT. These can be combined with the bitwise OR operator | to press multiple modifiers at once.

DigiKeyboard key codes and commands

The DigiKeyboard library defines numerical codes for every key. You specify a key by its code or by using the defined constants. Below is a structured list of common keys and their corresponding codes. If you need a key that isn’t defined, you can define your own constant using the HID usage ID from the USB HID specification (for example, #define KEY_ESCAPE 0x29 defines the Escape key).

Modifier Keys

  • MOD_CONTROL_LEFT / MOD_CONTROL_RIGHT – hold the Control key
  • MOD_SHIFT_LEFT / MOD_SHIFT_RIGHT – hold the Shift key
  • MOD_ALT_LEFT / MOD_ALT_RIGHT – hold the Alt key
  • MOD_GUI_LEFT / MOD_GUI_RIGHT – hold the Windows or Command key

Alphabet Keys (A–Z)

  • KEY_A (4) through KEY_Z (29) – letters A to Z

Numeric Keys

  • KEY_0 (39) through KEY_9 (38) – digits 0–9

Function Keys

  • KEY_F1 (58) to KEY_F12 (69) – function keys F1–F12

Navigation and Editing

  • KEY_ENTER (40) – Return/Enter
  • KEY_SPACE (44) – Spacebar
  • KEY_ARROW_LEFT (0x50), KEY_ARROW_RIGHT (0x4F), KEY_ARROW_UP (0x52) and KEY_ARROW_DOWN (0x51) – arrow keys
  • KEY_BACKSPACE (0x2A), KEY_TAB (0x2B) and KEY_ESCAPE (0x29) – editing keys (define these constants yourself if not present in the header)
  • KEY_HOME, KEY_END, KEY_PAGE_UP, KEY_PAGE_DOWN, KEY_INSERT and KEY_DELETE – navigation keys

Numeric Keypad

  • KEYPAD_0–KEYPAD_9, KEYPAD_PERIOD, KEYPAD_DIVIDE, KEYPAD_MULTIPLY, KEYPAD_MINUS, KEYPAD_PLUS, KEYPAD_ENTER

Media and System Controls*

  • Playback: MEDIA_PLAY_PAUSE, MEDIA_STOP_CD, MEDIA_PREVIOUS_TRACK, MEDIA_NEXT_TRACK, MEDIA_REWIND, MEDIA_FAST_FORWARD
  • Volume: MEDIA_VOLUME_UP, MEDIA_VOLUME_DOWN, MEDIA_VOLUME_MUTE
  • Other controls: MEDIA_EJECT_CD, MEDIA_PLAY, MEDIA_PAUSE
  • Applications: MEDIA_WWW (open default browser), MEDIA_CALCULATOR, MEDIA_EMAIL, MEDIA_BROWSER_SEARCH, MEDIA_BROWSER_HOME, MEDIA_BROWSER_BACK, MEDIA_BROWSER_FORWARD
  • Special keys: KEY_PRINTSCREEN, KEY_SCROLLLOCK, KEY_PAUSE, KEY_NUM_LOCK

*Availability of media keys may depend on your version of the library and firmware. Some keys might require updating the library or using another HID library.

USB HID Devices – A Digispark inside a Black and Yellow Casing

Using modifiers to press multiple keys

To press combinations such as Ctrl+C or Alt+Tab, pass the modifier flags in the second argument to sendKeyStroke(). If you need to press more than one modifier, combine them with the bitwise OR | operator. Remember that the key you want to press goes in the first parameter, and the modifiers follow after the comma.

// Copy (Ctrl + C)
DigiKeyboard.sendKeyStroke(KEY_C, MOD_CONTROL_LEFT);

// Lock workstation (Windows + L)
DigiKeyboard.sendKeyStroke(KEY_L, MOD_GUI_LEFT);

// New folder (Ctrl + Shift + N)
DigiKeyboard.sendKeyStroke(KEY_N, MOD_CONTROL_LEFT | MOD_SHIFT_LEFT);

// Open Run dialog (Windows + R), type a command and press Enter
DigiKeyboard.sendKeyStroke(KEY_R, MOD_GUI_LEFT);
DigiKeyboard.print("notepad");
DigiKeyboard.sendKeyStroke(KEY_ENTER);

Tips and best practices

  • Send a dummy keystroke first: Many examples begin with DigiKeyboard.sendKeyStroke(0). This prevents the first real keystroke from being missed on some systems.
  • Use DigiKeyboard.delay() instead of delay(): This keeps the USB connection alive and prevents the host from thinking the keyboard has disconnected.
  • Define missing key codes: If a key isn’t defined in the library, create your own constant using the HID usage table. For example, #define KEY_ESCAPE 0x29 defines the Escape key.
  • Be mindful of keyboard layouts: The library is designed for U.S. keyboards. On other layouts some characters (e.g., colon or backslash) may be mis‑typed. Consider replacing problematic characters or modifying the lookup table accordingly.
  • Test carefully: When sending keystrokes to a host computer, there is no undo! Always test your scripts in a safe environment before using them on important systems.
  • Update firmware and library: Media keys and advanced functions may require the latest DigiKeyboard library or alternative HID libraries.

Project ideas and applications

DigiSpark’s ability to emulate a keyboard opens a world of creative possibilities. Here are some projects to inspire you:

  • Macro keypad: Build a custom keypad with buttons wired to DigiSpark. Assign each button to automate repetitive tasks such as copying, pasting or launching applications.
  • Batch file automation: Use the board to execute scripts that require keyboard input, like launching command‑line tools and typing commands.
  • Assistive technology: Create custom input devices tailored to individuals with disabilities.
  • Game controllers and macros: Design special controllers or macro devices for gaming.
  • Home automation interfaces: Combine DigiSpark with sensors or relays to control smart‑home devices by sending keyboard commands through a host PC.
  • Educational tools: Use DigiSpark to teach programming and electronics – it’s inexpensive and encourages experimentation.
  • Prototype development: Rapidly prototype devices that require keyboard or mouse input.

Security considerations – the “Rubber Ducky” effect

Because computers trust keyboards implicitly, a DigiSpark programmed as a HID can be used for both helpful automation and malicious attacks. “Rubber Ducky” attacks involve hiding a microcontroller in a USB stick and using it to inject commands when plugged into a victim’s computer. Such scripts might open a terminal, download malware or steal data. Always use DigiSpark responsibly and never plug unknown USB devices into your system. For ethical hacking or penetration testing, obtain proper authorization and follow applicable laws.

Conclusion

The DigiSpark board proves that powerful things come in small packages. With the DigiKeyboard library you can turn this inexpensive ATtiny85 module into a fully programmable USB keyboard. By understanding the available key codes, modifier flags and best practices, you can create custom macro pads, automation tools and innovative HID projects. Whether you’re a hobbyist automating tedious tasks or an educator teaching embedded programming, DigiSpark offers a flexible platform for exploration. Remember to experiment safely, and let your creativity guide you into the world of custom HID devices.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *