-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathkeyboard_retrieval.py
More file actions
44 lines (33 loc) · 1.32 KB
/
keyboard_retrieval.py
File metadata and controls
44 lines (33 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import logging
import os
from typing import Final, List
INPUT_DEVICES_PATH: Final = '/dev/input/by-id'
def retrieve_keyboard_name() -> str:
# List all devices in the directory
all_devices = os.listdir(INPUT_DEVICES_PATH)
keyboard_devices = [
d for d in all_devices
]
# Remove duplicates just in case
keyboard_devices = list(set(keyboard_devices))
n_devices = len(keyboard_devices)
if n_devices == 0:
raise ValueError(f"Couldn't find a keyboard in '{INPUT_DEVICES_PATH}'")
if n_devices == 1:
logging.info(f"Found keyboard: {keyboard_devices[0]}")
return keyboard_devices[0]
# Use native Python input for user selection
print("Select a device:")
for idx, device in enumerate(sorted(keyboard_devices), start=1):
print(f"{idx}. {device}")
selected_idx = -1
while selected_idx < 1 or selected_idx > n_devices:
try:
selected_idx = int(input("Enter your choice (number): "))
if selected_idx < 1 or selected_idx > n_devices:
print(f"Please select a number between 1 and {n_devices}")
except ValueError:
print("Please enter a valid number")
return keyboard_devices[selected_idx - 1]
def abs_keyboard_path(device: str) -> str:
return os.path.join(INPUT_DEVICES_PATH, device)