© Copyright, 2025 G. Schaer.

SPDX-License-Identifier: GPL-3.0-only

Keyboard Example

This module gives example usage of the keyboard.

[1]:
import time
from condynsate import Keyboard

Create an instance of the keyboard. Instantiation wil automatically start a keyboard listener thread

[2]:
keyboard = Keyboard()

Next we will run a loop that prints to the screen all keys the user is currently pressing. We do this with the get_pressed function. We will also listen for the user pressing the esc key at which point the loop will end. This is done with the is_pressed function. If the user does not press the esc key within 5 seconds, the loop will automatically exit.

[3]:
start = time.perf_counter()
print("Press 'esc' to end.")
while time.perf_counter()-start < 5.0:
    # If the esc key is ever pressed, end the loop
    if keyboard.is_pressed('esc'):
        print("esc' pressed. Quitting.")
        break

    # Get every pressed key. If any pressed keys are detected, print them
    pressed = keyboard.get_pressed()
    if len(pressed) > 0:
        print(f"Keys pressed: {pressed}")

    # Just to remove CPU strain
    time.sleep(0.05)
Press 'esc' to end.

When done, terminate ensures graceful exit of the listener thread

[4]:
keyboard.terminate() #Returns 0 on success
[4]:
0