Reputation:
I have been searching forums and the web for hours looking into a way of making a repeating python loop for Autokey. The goal is to allow timed intervals of key presses, such as pressing "1" every 16 seconds, "2" every 30, so on and so forth for as many keys as I may need (likely less than 8). I would also want to be able to end this process at the click of any combination. I have been testing looping only 1 keyboard input every 5 seconds, and I can easily make that work. I am fairly new to python and coding in general, and what has worked for me in the past does not here. I've tried:
import time
import sys
try:
while True:
time.sleep(5)
keyboard.send_key("4")
except KeyboardInterrupt:
exit(0)
sys.exit(0)
and variations there of, such as switching the while loop and try/except. It feels as though my keyboardinterrupt is not working properly, I've used ctrl -c and ctrl break, to no avail. Any help is appreciated. Thank you in advance.
Upvotes: 3
Views: 4722
Reputation: 11
I have a similar requirement, and from my searching, I found a comment from AutoKey developer.
These codes could be what you need:
while True:
retCode = keyboard.wait_for_keypress(
'c', modifiers=['<ctrl>'], timeOut=5)
if retCode:
break
keyboard.send_key("4")
Upvotes: 1
Reputation: 2642
KeyboardInterrupt
only catches Ctrl+C if it was send to the controlling terminal. This means that if you're pressing Ctrl+C from a different window, it will not catch.
To work around this, you need to register a keyboard shortcut on Ctrl+C, and have it send a signal to your main script.
Upvotes: 0