I am making an AI to drive cars in GTA San Andreas using Tensorflow and I want to know which characters/keys are pressed at each frame using python. I cannot use input()
cause my program is not in front. What I do?
Asked
Active
Viewed 971 times
1

ParmuTownley
- 957
- 2
- 14
- 34
1 Answers
2
pyHook might be something you might be looking for. All the keyboard or mouse events can be captured using Windows hooks. pyhook
is a Python wrapper around the hooks API.
This answer presents a sample code for using pyhook
to capture the keypress. This document provides with basics of Hook in Windows.
Below is an example, which hooks for keyboard events and prints the key pressed to console. It exits on keypress for x
or 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()

skjoshi
- 2,493
- 2
- 27
- 38
-
I think this will work, but can you give me a better explanation than in the link? Could you please just write the code to repeatedly print the pressed character? – ParmuTownley Jul 24 '17 at 17:11