[英]whats wrong with my script (key press, python)
I just wanted to make a simple script that key presses "4" seven times, if I press "4" once.我只想制作一个简单的脚本,如果我按一次“4”,则按键按“4”七次。
I want it to work in the background while, and work any time I key press "4"我希望它在后台工作,并且在我按下“4”时随时工作
here is what I tried这是我试过的
import pyautogui
import time
import msvcrt
while True:
key = msvcrt.getch().lower()
if key == b'4':
for x in range(1,7):
pyautogui.press(4)
time.sleep(0.3)
break
I tried to execute this on console and it didn't work.我试图在控制台上执行此操作,但没有成功。 No errors pop up, but no matter how many times I press 4, it doesn't make me press 4 7 more times.没有弹出错误,但无论我按 4 多少次,它都不会让我再按 4 7 次。 Any help is appreciated.任何帮助表示赞赏。
Try:尝试:
pyautogui.press('4')
From the documentation press takes strings, if it doesn't work you can also try:从文档中 press 获取字符串,如果它不起作用你也可以尝试:
while True:
if msvcrt.kbhit():
if str(msvcrt.getch()) == b'4':
for _ in range(7):
pyautogui.press('4')
Try this尝试这个
import keyboard
import pyautogui
import time
while True:
if keyboard.read_key() == "4":
for x in range(0,5):
pyautogui.press('4')
time.sleep(0.3)
break
If you use windows msvcrt could be a solution but we always try to be as universal as possible so I recommend to use a more apropriate library or module like pyautogui or keyboard to get pressed keys.如果你使用 windows msvcrt可能是一个解决方案,但我们总是尽量尽可能通用,所以我建议使用更合适的库或模块,如pyautogui或键盘来获取按下的键。
import keyboard
if keyboard.is_pressed('4'):
# do anything
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.