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

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

stuck on adding a timer to my keyboard press program

问题

  1. 我正在制作一个键盘程序每隔0.1秒重复按一个键并使用esc作为启动/停止程序的热键
  2. ```python
  3. import keyboard
  4. import time
  5. keyboard.wait('esc')
  6. name = ("python")
  7. while name == "python":
  8. keyboard.press_and_release('5')
  9. time.sleep(0.1)
  10. #这是我想要添加计时器的地方
  11. 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.

  1. import keyboard
  2. import time
  3. keyboard.wait('esc')
  4. name = ("python")
  5. while name == "python":
  6. keyboard.press_and_release('5')
  7. time.sleep(0.1)
  8. #this is where i want to add the timer
  9. 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

也许你可以尝试类似这样的代码:

  1. import keyboard
  2. import time
  3. keyboard.wait('esc')
  4. name = "python"
  5. timer = time.time()
  6. while name == "python":
  7. keyboard.press_and_release('5')
  8. time.sleep(0.1)
  9. timer2 = time.time()
  10. if timer2 - timer >= 5:
  11. name = "notpython"

请注意,代码部分没有被翻译。

英文:

maybe you can try something like this:

  1. import keyboard
  2. import time
  3. keyboard.wait('esc')
  4. name = ("python")
  5. timer = time.time()
  6. while name == "python":
  7. keyboard.press_and_release('5')
  8. time.sleep(0.1)
  9. timer2 = time.time()
  10. 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:

确定