在我的键盘按键程序中卡住了,想要添加一个定时器。

huangapple go评论60阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年2月14日 22:40:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定