【发布时间】:2017-07-24 16:02:32
【问题描述】:
我正在使用 Tensorflow 制作一个 AI 来在 GTA San Andreas 中驾驶汽车,我想知道使用 python 在每一帧按下了哪些字符/键。我不能使用input(),因为我的程序不在前面。我要做什么?
【问题讨论】:
我正在使用 Tensorflow 制作一个 AI 来在 GTA San Andreas 中驾驶汽车,我想知道使用 python 在每一帧按下了哪些字符/键。我不能使用input(),因为我的程序不在前面。我要做什么?
【问题讨论】:
pyHook 可能是您正在寻找的东西。可以使用 Windows 挂钩捕获所有键盘或鼠标事件。 pyhook 是 hooks API 的 Python 包装器。
This answer 提供了使用pyhook 捕获按键的示例代码。 This document 提供 Windows 中 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()
【讨论】: