我正在使用 Tensorflow 制作一个人工智能来在 GTA San Andreas 中驾驶汽车,我想知道使用 python 在每一帧按下了哪些字符/按键。我无法使用 input()
因为我的程序不在前面。我做什么?
最佳答案
pyHook可能是您正在寻找的东西。所有键盘或鼠标事件都可以使用 Windows Hook 捕获。 pyhook
是 hooks API 的 Python 包装器。
This answer提供了使用 pyhook 捕获按键的示例代码。 This document提供 Windows 中 Hook 的基础知识。
下面是一个示例,它 Hook 键盘事件并将按下的键打印到控制台。按下 x
或 X
按键即可退出。
#!python
import pythoncom, pyHook
import sys
def OnKeyboardEvent(event):
# block only the letter A, lower and uppercase
print chr(event.Ascii)
if event.Ascii in (ord('x'), ord('X')):
sys.exit()
# returning True to pass on event to other applications
return True
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
关于python - 使用python玩游戏时如何获取键盘输入的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45285207/