Overview
KeyboardMode is an abstract base class that extends InputMode to provide keyboard-specific functionality. It handles the conversion of physical button inputs into keyboard key presses using USB HID keyboard reports.
KeyboardMode is hardware-specific and is only available on platforms that support USB keyboard functionality (e.g., Raspberry Pi Pico). The header file location varies by HAL (Hardware Abstraction Layer).
Class Definition
Inheritance
Inherits from
InputMode, which provides configuration management, SOCD handling, and button remapping functionality.Constructor & Destructor
KeyboardMode()
- Calls the parent
InputModeconstructor - Creates a new
TUKeyboardinstance - Calls
_keyboard->begin()to initialize USB keyboard functionality
~KeyboardMode()
- Releases all pressed keys via
_keyboard->releaseAll() - Sends a final keyboard report to clear all keys
- Deletes the
TUKeyboardinstance
Public Methods
SendReport
Reference to the current input state containing all physical button presses
- Creates a copy of inputs for remapping
- Applies button remapping via
HandleRemap() - Applies SOCD (Simultaneous Opposite Cardinal Direction) resolution via
HandleSocd() - Calls
UpdateKeys()to set key press states - Sends the keyboard report via
_keyboard->sendState()
UpdateOutputs
InputMode interface. KeyboardMode uses SendReport() instead of the standard output state mechanism.
This method is a no-op for KeyboardMode because keyboard outputs are sent directly via USB HID reports rather than through the OutputState structure.
Protected Methods
Press
USB HID keyboard keycode (e.g.,
HID_KEY_A, HID_KEY_SHIFT_LEFT)true to press the key, false to release it_keyboard->setPressed(keycode, press) to update the internal keyboard state. The actual USB report is sent when SendReport() is called.
Example:
Pure Virtual Methods
UpdateKeys
Remapped and SOCD-resolved input state
- Call
Press()for each button-to-key mapping - The input state passed to this method has already been processed through remapping and SOCD
USB HID Keycodes
Keyboard modes use standard USB HID keycodes. Common keycodes include:HID_KEY_A through HID_KEY_Z (0x04-0x1D)HID_KEY_1 through HID_KEY_0 (0x1E-0x27)HID_KEY_SHIFT_LEFT(0xE1)HID_KEY_CONTROL_LEFT(0xE0)HID_KEY_ALT_LEFT(0xE2)HID_KEY_GUI_LEFT(0xE3) - Windows/Command key
HID_KEY_ENTER(0x28)HID_KEY_ESCAPE(0x29)HID_KEY_BACKSPACE(0x2A)HID_KEY_TAB(0x2B)HID_KEY_SPACE(0x2C)
HID_KEY_ARROW_RIGHT(0x4F)HID_KEY_ARROW_LEFT(0x50)HID_KEY_ARROW_DOWN(0x51)HID_KEY_ARROW_UP(0x52)
Usage Example
Here’s how theDefaultKeyboardMode implements KeyboardMode:
Implementation
Creating a Custom Keyboard Mode
Here’s an example of a custom keyboard mode for a specific game:Integration with Communication Backend
KeyboardMode works differently from ControllerMode in the main loop:Use a dynamic cast to check if the current mode is a KeyboardMode. This allows you to handle keyboard and controller modes differently in your main loop.
Platform Support
Full keyboard support via TinyUSB libraryHeader:
HAL/pico/include/core/KeyboardMode.hppFull keyboard support via LUFA libraryHeader:
HAL/avr/avr_usb/include/core/KeyboardMode.hppKeyboardMode is not available on AVR platforms without USB support
Best Practices
SOCD resolution and button remapping are automatically applied before
UpdateKeys() is called, so you don’t need to handle these in your implementation.Limitations
- Maximum of 6 simultaneous non-modifier keys (USB HID keyboard limitation)
- Modifier keys (Shift, Ctrl, Alt, GUI) are tracked separately and don’t count toward the 6-key limit
- Media keys and other special functions require specific HID descriptors in the TUKeyboard implementation
Related Types
- InputState: Defined in
include/core/state.hpp- contains all button states - InputMode: Parent class providing config, SOCD, and remapping functionality
- TUKeyboard: TinyUSB keyboard implementation (hardware-specific)
See Also
- ControllerMode - For implementing game controller modes
- InputSource - For reading physical inputs
- CommunicationBackend - For managing input/output operations
