API Input
Keyboard and button input functions.
picocalc.input
Section titled “picocalc.input”Functions
Section titled “Functions”picocalc.input.update()
Section titled “picocalc.input.update()”Polls the keyboard for new input events. Call once per frame before reading button or character state.
update() also services HTTP/TCP/sound callbacks, the system menu and dev commands.
- Parameters: None
- Returns: None
picocalc.input.update()picocalc.input.getButtons()
Section titled “picocalc.input.getButtons()”Returns the current bitmask of held buttons.
- Parameters: None
- Returns: (number) Bitmask of currently pressed buttons
local buttons = picocalc.input.getButtons()if buttons & picocalc.input.BTN_UP ~= 0 then -- Up button is heldendpicocalc.input.getButtonsPressed()
Section titled “picocalc.input.getButtonsPressed()”Returns the bitmask of buttons that were pressed this frame (edge detection, not held).
- Parameters: None
- Returns: (number) Bitmask of buttons pressed this frame
local pressed = picocalc.input.getButtonsPressed()if pressed & picocalc.input.BTN_ENTER ~= 0 then -- Enter was just pressedendpicocalc.input.getButtonsReleased()
Section titled “picocalc.input.getButtonsReleased()”Returns the bitmask of buttons that were released this frame.
- Parameters: None
- Returns: (number) Bitmask of buttons released this frame
picocalc.input.getButtonsRepeated()
Section titled “picocalc.input.getButtonsRepeated()”Like getButtonsPressed(), but buttons held past the repeat delay also produce synthetic repeat edges (see setRepeat()). Useful for menus that should scroll while a button is held.
- Parameters: None
- Returns: (number) Bitmask of pressed edges plus synthetic repeat edges
picocalc.input.setRepeat(400, 80) -- start repeating after 400ms, then every 80mslocal pressed = picocalc.input.getButtonsRepeated()if pressed & picocalc.input.BTN_DOWN ~= 0 then selection = selection + 1 -- scrolls while heldendpicocalc.input.setRepeat(delay_ms [, rate_ms])
Section titled “picocalc.input.setRepeat(delay_ms [, rate_ms])”Configures key auto-repeat for getButtonsRepeated().
- Parameters:
delay_ms(number): Milliseconds a button must be held before repeating starts.0disables auto-repeat.rate_ms(number, optional): Milliseconds between repeat edges (default80, minimum1).
- Returns: None
picocalc.input.setRepeat(400) -- repeat every 80ms after a 400ms holdpicocalc.input.setRepeat(0) -- disable auto-repeatpicocalc.input.getChar()
Section titled “picocalc.input.getChar()”Returns the last ASCII character typed, if any.
- Parameters: None
- Returns: (string or nil) Single-character string, or
nilif no character was typed this frame
local ch = picocalc.input.getChar()if ch then text = text .. chendpicocalc.input.getRawKey()
Section titled “picocalc.input.getRawKey()”Returns the raw STM32 keycode from the keyboard controller.
- Parameters: None
- Returns: (number) Raw key code
picocalc.input.clearState()
Section titled “picocalc.input.clearState()”Clear all button and key input state. Resets pressed, released, and held button buffers, queued pollEvent events and isKeyDown state. Useful when returning from modal dialogs or menus to prevent stale input.
- Parameters: None
- Returns: None
-- After closing a dialog, clear stale inputpicocalc.input.clearState()picocalc.input.pollEvent()
Section titled “picocalc.input.pollEvent()”Pops the oldest keyboard event, or nil when none is queued. Events are
filled by update() in the order keys were pressed and released, so taps
shorter than a frame and several characters in one frame are all reported.
- Returns: (table or nil)
{type, key, char, mods, button, repeat}:type:"down","up"or"char"key: ASCII for printable keys, else the keyboard code (asgetRawKey())char: the character ("char"events only)mods:BTN_SHIFT/BTN_CTRL/BTN_ALT/BTN_FNheld at the eventbutton: theBTN_*constant for keys that have onerepeat:truefor events produced by holding the key (read asev["repeat"])
Independent of getChar()/getButtons*(). 16 events are kept (the oldest is
dropped); a new app starts with an empty queue.
picocalc.input.update()for ev in picocalc.input.pollEvent do if ev.type == "char" then text = text .. ev.char endendpicocalc.input.isKeyDown(k)
Section titled “picocalc.input.isKeyDown(k)”True while a key is held. k is a one-character string ("w"; letters
ignore case) or an integer keycode as pollEvent reports it. Updated by
update(). Reliable for buttons (arrows, Enter, Esc, F-keys, modifiers);
letters and shifted symbols depend on the keyboard reporting their release
(pending hardware confirmation). clearState() clears a key that sticks.
Button Constants
Section titled “Button Constants”Bitmask values for button states:
| Constant | Description |
|---|---|
picocalc.input.BTN_UP | D-pad Up |
picocalc.input.BTN_DOWN | D-pad Down |
picocalc.input.BTN_LEFT | D-pad Left |
picocalc.input.BTN_RIGHT | D-pad Right |
picocalc.input.BTN_ENTER | Enter key |
picocalc.input.BTN_ESC | Escape key |
picocalc.input.BTN_MENU | Menu key (system overlay, auto-handled) |
picocalc.input.BTN_F1 | F1 key |
picocalc.input.BTN_F2 | F2 key |
picocalc.input.BTN_F3 | F3 key |
picocalc.input.BTN_F4 | F4 key |
picocalc.input.BTN_F5 | F5 key |
picocalc.input.BTN_F6 | F6 key |
picocalc.input.BTN_F7 | F7 key |
picocalc.input.BTN_F8 | F8 key |
picocalc.input.BTN_F9 | F9 key |
picocalc.input.BTN_BACKSPACE | Backspace key |
picocalc.input.BTN_TAB | Tab key |
picocalc.input.BTN_DEL | Delete key |
picocalc.input.BTN_SHIFT | Shift modifier (left or right) |
picocalc.input.BTN_CTRL | Ctrl modifier |
picocalc.input.BTN_ALT | Alt modifier |
picocalc.input.BTN_FN | Fn/Symbol modifier |