英文:
stuck on adding a timer to my keyboard press program
问题
我正在制作一个键盘程序,每隔0.1秒重复按一个键,并使用esc作为启动/停止程序的热键。
```python
import keyboard
import time
keyboard.wait('esc')
name = ("python")
while name == "python":
keyboard.press_and_release('5')
time.sleep(0.1)
#这是我想要添加计时器的地方
name = ("notpython")
我想在那里添加计时器,以便在几秒钟后name变量从python变为notpython,并使while循环变为false。
我尝试了time.sleep函数,但它不停地打印5并且不停止。
英文:
Im making a keyboard program that repeatedly presses a key every 0.1 seconds and uses esc as the hotkey to stop/start the program.
import keyboard
import time
keyboard.wait('esc')
name = ("python")
while name == "python":
keyboard.press_and_release('5')
time.sleep(0.1)
#this is where i want to add the timer
name = ("notpython")
I want to add the timer there so that after a few seconds the name variable changes from python to notpython and making the while loop false.
I've tried the time sleep function but it keeps printing 5 and doesnt stop.
答案1
得分: 0
也许你可以尝试类似这样的代码:
import keyboard
import time
keyboard.wait('esc')
name = "python"
timer = time.time()
while name == "python":
keyboard.press_and_release('5')
time.sleep(0.1)
timer2 = time.time()
if timer2 - timer >= 5:
name = "notpython"
请注意,代码部分没有被翻译。
英文:
maybe you can try something like this:
import keyboard
import time
keyboard.wait('esc')
name = ("python")
timer = time.time()
while name == "python":
keyboard.press_and_release('5')
time.sleep(0.1)
timer2 = time.time()
if timer2 - timer >= 5: name = "notpython"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论