deckle studioOpen in the editor

Pro Micro: USB macro keyboard

Four buttons type text and shortcuts on the PC.

  • Pro Micro (ATmega32U4)
Open in the simulatorNo account needed: press Simulate and it runs.

How it works

The Pro Micro's ATmega32U4 has USB built in: the sketch makes it a keyboard and mouse for the PC (Keyboard.h, Mouse.h). Button SW1 on D2 types a greeting, SW2 on D3 sends Ctrl+C, SW3 on D4 Ctrl+V, SW4 on D5 moves the mouse in a square and clicks. Press the buttons while the simulation runs: the serial monitor shows what the PC receives (Keyboard / Mouse) and, over the USB serial port, which button was pressed. The board is powered and connected over USB.

Wiring

Each line is one connection (net) and the pins it joins.

NetPins
GNDA1 GNDSW1 2SW2 2SW3 2SW4 2
B1A1 D2/SDASW1 1
B2A1 ~D3/SCLSW2 1
B3A1 D4SW3 1
B4A1 ~D5SW4 1

Parts

Firmware

sketch.ino · 51 lines

// Pro Micro (ATmega32U4) as a macro keyboard: the board is a USB keyboard and mouse for the PC.
//  - button on D2: types a greeting
//  - button on D3: Ctrl+C, button on D4: Ctrl+V
//  - button on D5: moves the mouse in a small square and clicks
// The buttons switch to GND (internal pull-ups). The console shows what the PC receives
// ("Keyboard ▸" / "Mouse ▸") and, over the USB serial port, which button was pressed.
#include <Keyboard.h>
#include <Mouse.h>

const byte BUTTONS[4] = { 2, 3, 4, 5 };
bool was[4];
unsigned long lastChange[4];

void setup() {
  for (byte i = 0; i < 4; i++) { pinMode(BUTTONS[i], INPUT_PULLUP); was[i] = HIGH; }
  Keyboard.begin();
  Mouse.begin();
  Serial.begin(9600);
}

void shortcut(char key) {
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(key);
  delay(20);
  Keyboard.releaseAll();
}

void square() {
  const int dx[4] = { 40, 0, -40, 0 }, dy[4] = { 0, 40, 0, -40 };
  for (byte side = 0; side < 4; side++)
    for (byte step = 0; step < 4; step++) { Mouse.move(dx[side] / 4, dy[side] / 4); delay(10); }
  Mouse.click();
}

void loop() {
  for (byte i = 0; i < 4; i++) {
    bool now = digitalRead(BUTTONS[i]);
    if (now != was[i] && millis() - lastChange[i] > 30) {      // debounce
      lastChange[i] = millis();
      was[i] = now;
      if (now == LOW) {                                        // pressed
        Serial.print("button on D");
        Serial.println(BUTTONS[i]);
        if (i == 0) Keyboard.println("Hello from the Pro Micro!");
        if (i == 1) shortcut('c');
        if (i == 2) shortcut('v');
        if (i == 3) square();
      }
    }
  }
}
Open in the simulator

More examples: Arduino