Go back

Building the Stack Overflow Three-Key Macro Keyboard with ATmega32U4

Published:  at  12:35 AM
Updated:  at  04:10 AM
⏱️ 994 words • 5 min read

阅读中文版

Recreating Stack Overflow's April Fools' three-key macro keyboard with ATmega32U4 USB HID, a KiCad PCB, and Arduino keyboard shortcuts.

background

On April Fool’s Day 2021, Stack Overflow made a very interesting easter egg: limiting the number of copies users can make, and “promoting” a customized keyboard with only three keys.

This small and elegant keyboard inspires my creative urge. After thinking about it, I realized that the implementation logic is actually very simple. Basically, I just slightly modified the “Genshin Impact Automatic Piano Playing Device” I made before. The core code is even within a hundred lines. Keyboard by StackOverflow

Then I opened KiCad and started working directly, from schematic to 3D rendering in one go.

🛠️ Core design idea: Why ATMega32u4?

Before starting production, selection is key. The keyboard in the official Stack Overflow easter egg only has three keys: Ctrl, C, V.

In order to achieve this function, I chose ATMega32u4 as the master:

  • Native HID support: Different from ordinary ESP32 or ATMega328P, 32u4 comes with hardware USB support and can be directly recognized by the computer as a standard keyboard (HID terminal) without the need for an additional serial port to USB chip.
  • Mature firmware library: Using Arduino’s Keyboard.h library, you can implement key combination logic extremely quickly.

📐 Hardware Development: KiCad Process and PCB Details

In KiCad, I designed a minimalist 3% layout circuit board.

rendering effect

Rendering - Keyboard Rendering - Keyboard

Key circuit characteristics

  1. Hot-swappable support: introduces Kaihua hot-swappable shaft base (1511 series), which means you can replace the green shaft, red shaft or silent shaft at any time.
  2. Power management: uses the AP2112K-3.3 LDO chip to ensure that 32u4 works at a stable voltage, and adds a Schottky diode to prevent reverse power supply from the Type-C interface.
  3. Mode switching: Added a 2-digit DIP switch. Not only can this be used to switch between different keymaps (like the Mac/Win switch), but it can also turn “burst mode” on or off.

💻 Software implementation: combination key logic

The core logic of the firmware is to map the level changes of physical keys to standard HID coding. The logic to implement Ctrl + C is as follows:

// 示例:实现 Ctrl + C 的伪代码
if (digitalRead(KEY_C_PIN) == LOW) {
    Keyboard.press(KEY_LEFT_CTRL); // 先按下 Ctrl
    Keyboard.press('c');           // 再按下 C
    delay(100);                    // 保持一小段时间
    Keyboard.releaseAll();         // 释放所有按键
}

Bill of Materials (BOM)

Preview

Serial numberNameQuantityRemarks
1ATMega32u41Main control chip QFN44 package
2AP2112K-3.315V to 3.3V chip SOT-23-5
316Mhz crystal oscillator12.0*1.6
41uF capacitor10603
522pf capacitor20603
610uf capacitor20603
71KΩ resistor50805
810KΩ resistor10805
922Ω resistor20805
10Kaihua hot-swappable shaft seat31511 series
11Shaft3Any shaft
122-digit DIP switch11.27mm
13Type-C interface116p
14Schottky Diode1BAT60JFILM SOD-323
15Micro switch13x4x2.5 four-pin patch

Soldering and assembly

Soldering experience: Although the QFN44 package has dense pins, automatic alignment and adsorption can be achieved after applying an appropriate amount of solder paste and using a hot air gun (recommended 350°C, medium wind speed).

PCB circuit board

Assembly of: I simply drew a square box as the casing, then snapped three keycaps from my old keyboard and put them on.

Actual photos Actual photos

Advanced features and open source

Although this keyboard is derived from an Easter egg, it is highly practical:

Burst mode: turned on by DIP switch, suitable for certain scenarios that require rapid repeated operations.

Compatible with all platforms: Based on the standard HID protocol, plug and play, no driver installation required.

Open source address: https://github.com/chaosgoo/HairsPP

Attached: HID Scanning Code Cheat Sheet (Part)

In order to facilitate customizing keys, I have compiled commonly used non-standard key codes. You can modify the firmware to realize macro functions such as F13-F24.

Control and Modifier Keys

DefinitionEncoding (Hex)Functional Description
KEY_LEFT_CTRL0x80Left Ctrl key
KEY_LEFT_SHIFT0x81Left Shift key
KEY_LEFT_ALT0x82Left Alt key
KEY_LEFT_GUI0x83Left Win/Command key
KEY_RIGHT_CTRL0x84Right Ctrl key
KEY_RIGHT_SHIFT0x85Right Shift key
KEY_RIGHT_ALT0x86Right Alt key
KEY_RIGHT_GUI0x87Right Win/Command key

System & Navigation

DefinitionEncoding (Hex)Functional Description
KEY_UP_ARROW0xDAArrow keys: Up
KEY_DOWN_ARROW0xD9Arrow keys: Down
KEY_LEFT_ARROW0xD8Arrow key: Left
KEY_RIGHT_ARROW0xD7Arrow key: Right
KEY_BACKSPACE0xB2Backspace
KEY_TAB0xB3Tab key (Tab)
KEY_RETURN0xB0Enter key (Enter)
KEY_ESC0xB1Escape key (Esc)
KEY_INSERT0xD1Insert key (Insert)
KEY_DELETE0xD4Delete key (Delete)
KEY_PAGE_UP0xD3Page Up key (Page Up)
KEY_PAGE_DOWN0xD6Page Down key (Page Down)
KEY_HOME0xD2Home key (Home)
KEY_END0xD5End key (End)
KEY_CAPS_LOCK0xC1Caps Lock

Function Keys

DefinitionEncoding (Hex)Functional Description
KEY_F1 - KEY_F120xC2 - 0xCDStandard function keys F1 to F12
KEY_F13 - KEY_F240xF0 - 0xFBExtended function keys (often used to bind macro commands and will not conflict with system shortcut keys)

Summary and Outlook

Although this 3-key macro keyboard was copied from an April Fool’s Easter egg on Stack Overflow, it was originally intended for fun, but in the actual production process, it covered many core knowledge points such as native USB-HID communication, hardware circuit design, and ergonomic key mapping.

Through this project, we not only implemented a minimalist productivity tool, but also gained a deeper understanding of:

  • The importance of main control selection: ATMega32u4’s hardware USB support is the key to achieving low latency and high compatibility HID devices.
  • Advantages of modular design: The introduction of hot-swappable shaft bases and DIP switches gives a compact piece of hardware a high degree of customization.
  • The charm of combining software and hardware: A few simple lines of code, combined with precise hardware triggering, can greatly improve the efficiency of high-frequency operations in daily office work (such as cutting and pasting, code reconstruction, and macro command execution).

Share this post on:

Previous Post
Variable Fonts in LVGL, Part 1: Getting Started with FreeType
Next Post
Displaying a Remote JPG Image on ESP32 over HTTP