Reference

sshkeyboard

sshkeyboard.listen_keyboard(on_press=None, on_release=None, until='esc', sequential=False, delay_second_char=0.75, delay_other_chars=0.05, lower=True, debug=False, max_thread_pool_workers=None, sleep=0.01)[source]

Listen for keyboard events and fire on_press and on_release callback functions

Blocks the thread until the key in until parameter has been pressed, an error has been raised or stop_listening() has been called.

Supports asynchronous callbacks also.

Example:

from sshkeyboard import listen_keyboard

async def press(key):
    print(f"'{key}' pressed")

def release(key):
    print(f"'{key}' released")

listen_keyboard(
    on_press=press,
    on_release=release,
)
Parameters
  • on_press (Optional[Callable[[str], Any]]) – Function that gets called when a key is pressed. The function takes the pressed key as parameter. Defaults to None.

  • on_release (Optional[Callable[[str], Any]]) – Function that gets called when a key is released. The function takes the released key as parameter. Defaults to None.

  • until (Optional[str]) – A key that will end keyboard listening. None means that listening will stop only when stop_listening() has been called or an error has been raised. Defaults to “esc”.

  • sequential (bool) – If enabled, callbacks will be forced to happen one by one instead of concurrently or asynchronously. Defaults to False.

  • delay_second_char (float) – The timeout between first and second character when holding down a key. Depends on terminal and is used for parsing the input. Defaults to 0.75.

  • delay_other_chars (float) – The timeout between all other characters when holding down a key. Depends on terminal and is used for parsing the input. Defaults to 0.05.

  • lower (bool) – If enabled, the callback ‘key’ parameter gets turned into lower case key even if it was upper case, for example “A” -> “a”. Defaults to True.

  • debug (bool) – Print debug messages. Defaults to False.

  • max_thread_pool_workers (Optional[int]) – Define the number of workers in ThreadPoolExecutor, None means that a default value will get used. Will get ignored if sequential=True. Defaults to None.

  • sleep (float) – asyncio.sleep() amount between attempted keyboard input reads. Defaults to 0.01.

Return type

None

async sshkeyboard.listen_keyboard_manual(on_press=None, on_release=None, until='esc', sequential=False, delay_second_char=0.75, delay_other_chars=0.05, lower=True, debug=False, max_thread_pool_workers=None, sleep=0.01)[source]

The same as listen_keyboard(), but now the awaiting must be handled by the caller

from sshkeyboard import listen_keyboard_manual
# ...
asyncio.run(listen_keyboard_manual(...))

is the same as

from sshkeyboard import listen_keyboard
# ...
listen_keyboard(...)

(Python version 3.6 which does not have asyncio.run is handled differently internally)

Has the same parameters as listen_keyboard()

Return type

None

sshkeyboard.stop_listening()[source]

Stops the ongoing keyboard listeners

Can be called inside the callbacks or from outside. Does not do anything if listener is not running.

Return type

None