Skip to main content

! Press a hardware keyboard key (:press)

This method is available in versions after 20260805

Declaration

success, err = keyboard:press(key[, press_delay_ms])
success, err = keyboard:press(usage_page, usage_id[, press_delay_ms])

Parameters

  • key String. Uses the same key-name mapping as key.press.
  • usage_page Integer. Hardware keyboard objects currently accept only Keyboard/Keypad Usage Page 0x07.
  • usage_id Integer. Physical key identifier, not a character encoding.
  • press_delay_ms Number. Optional. How long to hold the key, in milliseconds. Default: 10 milliseconds.

Returns

  • success Boolean. Returns true when the key is pressed and released successfully and false on failure.
  • err String. Error message returned when the operation fails.

Description

Press the target key, wait for the specified duration, and then release it. Waiting may yield; other threads may run before the method returns.

  • This method preserves ordinary keys and modifier keys that were already held before the call.
  • If the same hardware keyboard is already running a :press or :send_text sequence, the new operation returns false, "hardware keyboard is busy".
  • Failed input is not retried through another input method.

Example

local keyboard = assert(key.connect_keyboard())

assert(keyboard:press("a"))
assert(keyboard:press(0x07, 4, 30)) -- Keyboard A, held for 30 ms

assert(keyboard:disconnect())