Last active
January 10, 2024 00:28
-
-
Save DiTo97/b4b9a4a0310ab7c60048a95f3c316179 to your computer and use it in GitHub Desktop.
It detects simultaneous keyboard presses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import keyboard | |
def get_pressed_keys() -> set[str]: | |
"""It obtains the pressed keyboard keys""" | |
keyboard._listener.start_if_necessary() | |
with keyboard._pressed_events_lock: | |
return set(map(lambda event: event.name, keyboard._pressed_events.values())) | |
def record_pressed_keys(until: str, framerate: int = 30) -> list[tuple[float, set[str]]]: | |
"""It records all pressed keyboard keys until the hotkey is pressed""" | |
assert framerate > 0, "The framerate has to be greater than 0" | |
recording = [] | |
frequency = 1 / framerate | |
until = keyboard.normalize_name(until) | |
start = time.perf_counter() | |
while True: | |
pressed_keys = get_pressed_keys() | |
if pressed_keys: | |
if until in pressed_keys: break | |
recording.append((time.perf_counter() - start, pressed_keys)) | |
time.sleep(frequency) | |
return recording | |
if __name__ == "__main__": | |
recording = record_pressed_keys(until="escape") | |
print(recording) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment