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)[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.

Example:

from sshkeyboard import listen_keyboard

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

listen_keyboard(on_press=press)
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. 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.

sshkeyboard.listen_keyboard_async(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.05)[source]

The same function as listen_keyboard(), but now the on_press and on_release callbacks are allowed to be asynchronous, and has a new sleep parameter

The new parameter sleep defines a timeout between starting the callbacks.

For the asynchronous callbacks, parameter sequential defines whether a callback is awaited or not before starting the next callback.

Example:

from sshkeyboard import listen_keyboard_async

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

listen_keyboard_async(on_press=press)
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. 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 starting the callbacks. None will remove the sleep altogether. Defaults to 0.05.

async sshkeyboard.listen_keyboard_async_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.05)[source]

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

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

is the same as

from sshkeyboard import listen_keyboard_async
# ...
listen_keyboard_async(...)

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

Has the same parameters as listen_keyboard_async()

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.