{ "cells": [ { "cell_type": "markdown", "id": "537031b2-2457-4568-b7af-8d1be152f5d5", "metadata": {}, "source": [ "© Copyright, 2025 G. Schaer.\n", "\n", "SPDX-License-Identifier: GPL-3.0-only" ] }, { "cell_type": "markdown", "id": "a7016d19-b420-46d2-a2b3-66731e383533", "metadata": {}, "source": [ "# Keyboard Example" ] }, { "cell_type": "markdown", "id": "2e3a837b-a3b2-4f99-8f71-f194c55326b8", "metadata": {}, "source": [ "This module gives example usage of the keyboard." ] }, { "cell_type": "code", "execution_count": 1, "id": "3608b839-c2bf-4aa0-a475-59f0f3ef4705", "metadata": {}, "outputs": [], "source": [ "import time\n", "from condynsate import Keyboard" ] }, { "cell_type": "markdown", "id": "3c132070-6a2e-485b-979e-9361054ac357", "metadata": {}, "source": [ "Create an instance of the keyboard. Instantiation wil automatically start a keyboard listener thread" ] }, { "cell_type": "code", "execution_count": 2, "id": "cbda3a4f-0c6a-4f96-9bf6-1940175e190c", "metadata": {}, "outputs": [], "source": [ "keyboard = Keyboard()" ] }, { "cell_type": "markdown", "id": "49a0d708-b989-4c1d-8be8-9dcad34775c8", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 3, "id": "d033a803-1560-4557-97c8-88470a6c06b0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Press 'esc' to end.\n" ] } ], "source": [ "start = time.perf_counter()\n", "print(\"Press 'esc' to end.\")\n", "while time.perf_counter()-start < 5.0:\n", " # If the esc key is ever pressed, end the loop\n", " if keyboard.is_pressed('esc'):\n", " print(\"esc' pressed. Quitting.\")\n", " break\n", "\n", " # Get every pressed key. If any pressed keys are detected, print them\n", " pressed = keyboard.get_pressed()\n", " if len(pressed) > 0:\n", " print(f\"Keys pressed: {pressed}\")\n", "\n", " # Just to remove CPU strain\n", " time.sleep(0.05)" ] }, { "cell_type": "markdown", "id": "a21ac189-9c30-4b1f-801b-fd5579eea408", "metadata": {}, "source": [ "When done, terminate ensures graceful exit of the listener thread" ] }, { "cell_type": "code", "execution_count": 4, "id": "6e3ae628-4f93-40b4-80c9-b1eecfe81266", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "keyboard.terminate() #Returns 0 on success" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.0" } }, "nbformat": 4, "nbformat_minor": 5 }