我目前正在编写一个脚本,该脚本按下“w,a,s,d”键以便在任何游戏中移动角色。 为此,我需要按住“w”键一段特定的时间。我怎样才能做到这一点?
我想到了这样的事情:
pyautogui.keyDown('w')
time.sleep(2)
pyautogui.keyUp('w')
但这只是暂停了整个程序,没有按键被按下,所以这对我没有用。
最佳答案
如 pyautogui.keyDown()
的文档字符串中所述:
Performs a keyboard key press without the release. This will put that key in a held down state.
NOTE: For some reason, this does not seem to cause key repeats like would happen if a keyboard key was held down on a text field.
您需要一种不同的方法 - 您可以使用 pygame - with this
或者,如果你想继续使用 pyautogui
,你可以尝试这样的事情:
def hold_W (hold_time):
import time, pyautogui
start = time.time()
while time.time() - start < hold_time:
pyautogui.press('w')
关于python - PyAutoGui - 按键 X 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48682388/