-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_presser.py
More file actions
65 lines (56 loc) · 1.72 KB
/
key_presser.py
File metadata and controls
65 lines (56 loc) · 1.72 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pyautogui
import time
import sys
def press_and_hold(key, duration):
"""Press and hold a key for specified duration in seconds."""
print(f"Pressing and holding '{key}' for {duration} seconds...")
pyautogui.keyDown(key)
time.sleep(duration)
pyautogui.keyUp(key)
print(f"Released '{key}'")
def press_and_release(key, delay):
"""Press and release a key after a specified delay in seconds."""
print(f"Waiting {delay} seconds before pressing '{key}'...")
time.sleep(delay)
pyautogui.press(key)
print(f"Pressed and released '{key}'")
def main():
print("=== Keyboard Key Presser ===\n")
# Get the key to press
key = input("Enter the key to press: ").strip()
if not key:
print("Error: No key specified")
sys.exit(1)
# Get the mode
print("\nSelect mode:")
print("1. Press and hold for X seconds")
print("2. Wait X seconds, then press and release")
mode = input("Enter mode (1 or 2): ").strip()
# Get the duration
try:
duration = float(input("Enter duration in seconds: "))
if duration <= 0:
print("Error: Duration must be positive")
sys.exit(1)
except ValueError:
print("Error: Invalid duration")
sys.exit(1)
# Execute based on mode
print()
if mode == "1":
press_and_hold(key, duration)
elif mode == "2":
press_and_release(key, duration)
else:
print("Error: Invalid mode selected")
sys.exit(1)
print("\nDone!")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\nOperation cancelled by user")
sys.exit(0)
except Exception as e:
print(f"\nError: {e}")
sys.exit(1)