Inputronic Keyboard - Live typing to Serial
Overview
This example demonstrates live typing using the Inputronic Keyboard with output to the serial console. Characters are printed in real-time as you type, making this ideal for terminal-style input and debugging.
Initialization
Import the module, create the keyboard object, and initialize it. If the keyboard isn't found, the program halts.
from inputronic_keyboard import InputronicKeyboard
from time import sleep
kbd = InputronicKeyboard()
if not kbd.begin():
print("Keyboard not found!")
else:
print("Keyboard ready! Start typing...")
Handling Special Keys
Inside the main loop, after reading each key event with readMappedEvent(), special keys are handled by comparing the label string. Release events and modifier-only keys (CAPS, SHIFT) are skipped.
if label == "SPACE":
print(" ", end="")
continue
if label == "BACK":
print("\b \b", end="") # Terminal backspace
continue
if label == "ENTER":
print() # New line
continue
# CAPS and SHIFT are modifiers only
if label in ["CAPS", "SHIFT"]:
continue
Key Behavior
| Key | Action |
|---|---|
| Printable keys | Printed to console |
| SPACE | Prints space character |
| BACK | Terminal-style backspace (\b \b) |
| ENTER | Prints newline |
| CAPS | Toggles upper/lower keymap (internal) |
| SHIFT | Modifier (handled automatically) |
Printing Characters
For all remaining keys, labelToChar() converts the label to a printable character with SHIFT applied automatically, and prints it to the console.
char = kbd.labelToChar(label, applyShift=True)
if char:
print(char, end="")
kbd.labelToChar(label, applyShift)
Converts a single-character label to a printable character. Automatically applies SHIFT and CAPS logic.
Returns value: String containing a single character, or None if the label is not a printable character
Function parameters:
| Type | Name | Description |
|---|---|---|
str | label | Key label from the keymap |
bool | applyShift | Whether to apply SHIFT transformations (default: True) |
SHIFT and CAPS logic is handled internally by the module. When SHIFT is held:
- Letters have their case inverted (CAPS XOR SHIFT)
- Numbers and symbols use the shift map (e.g., '1' becomes '!')
String Input Helper
The module also provides a convenient blocking stringInput() function for collecting text in one call:
text = kbd.stringInput(maxLen=64, endLabel="ENTER", backspaceLabel="BACK")
print("You typed: {}".format(text))
kbd.stringInput(maxLen, timeout, endLabel, backspaceLabel)
Blocking function that collects keyboard input until endLabel is pressed or timeout expires
Returns value: String containing all typed characters
Function parameters:
| Type | Name | Description |
|---|---|---|
int | maxLen | Maximum string length (default: 64) |
int | timeout | Timeout in milliseconds, 0=no timeout (default: 0) |
str | endLabel | Key that ends input (default: 'ENTER') |
str | backspaceLabel | Key used for backspace (default: 'BACK') |
Additional Functions
Get Last Pressed Key
label = kbd.getLastKeyLabel()
if label:
print("Last key pressed: {}".format(label))
kbd.getLastKeyLabel()
Returns the label of the last key that was pressed
Returns value: String containing the key label, or None if no key has been pressed
Check Active Keymap
keymap = kbd.getActiveKeymap()
# Returns 0 for UPPER, 1 for LOWER
print("Current keymap: {}".format("UPPER" if keymap == 0 else "LOWER"))
kbd.getActiveKeymap()
Returns the currently active keymap ID
Returns value: Integer: 0 for UPPER keymap, 1 for LOWER keymap
Count Held Keys
count = kbd.getHeldCount()
print("{} keys are currently pressed".format(count))
kbd.getHeldCount()
Returns the number of keys currently being held down
Returns value: Integer representing the count of pressed keys
Full Example
serial_type.py
Demonstrates live typing with the Inputronic Keyboard, printing characters to the console in real-time with support for SPACE, BACK, ENTER, SHIFT, and CAPS keys.