Untitled
unknown
plain_text
a year ago
2.0 kB
7
Indexable
from keyboard import on_press_key, wait
import threading
import subprocess
import time
# Variable to store valid keys
valid_keys = {"7", "8", "9", "4"}
# Variable to track if a script is running
script_running = False
# Function to handle keypress events
def on_press(key):
global script_running
try:
if script_running:
return # Ignore keypresses while a script is running
if key in valid_keys: # Check if the key is valid
print(f"Valid key pressed: {key}")
script_running = True
# Map keys to specific Python scripts and arguments
script_mapping = {
"7": ("Surroundings.py", []),
"8": ("Read_Text.py", []),
"9": ("Read_Text.py", ["-a"]),
"4": ("Audio.py", []),
}
# Get the script and arguments for the pressed key
script_to_run, script_args = script_mapping[key]
print(f"Running script: {script_to_run} with arguments: {script_args}")
# Run the corresponding script with arguments
subprocess.run(["python", script_to_run] + script_args) # Blocking call
# Mark as not running after the script ends
script_running = False
except AttributeError:
pass # Ignore special keys like Shift, Ctrl, etc.
def on_release(key):
global script_running
if script_running:
return False # Stop listener
# Function to start listening for keypresses
def start_listener():
for key in valid_keys:
on_press_key(key, lambda e, k=key: on_press(k))
wait()
# Start the listener in a background thread
listener_thread = threading.Thread(target=start_listener, daemon=True)
listener_thread.start()
# Main program doing other tasks
try:
while True:
time.sleep(1) # Simulate work
except KeyboardInterrupt:
print("\nExiting program.")
Editor is loading...
Leave a Comment