Keyboard Input

What method you guys use for checking keyboard input in python? There are many ways to do this, but which one do you recommend. Do you use and check the numbers? Do you have a sensor for each individual key? Do you use the GameKeys module? Do you use gameLogic.KX_INPUT_JUST_ACTIVATED? What is the best way to do it, or what are your suggestions?

So far I’ve just been using the method, and it works pretty good, so what is the point of the GameKeys module besides converting keycodes into strings and vise-versa?

Not exactly what you are asking, but in my latest project I made a command object that I store in GameLogic.

Anytime i switch scenes, I call GameLogic.command.listen( keySensor )

Then every frame i call a check_keys function.

From there, it matches the GameKeys code to a dictionary of command names, and spits back something easy to remember.

if “PAUSE” in GameLogic.command.check_keys():

anyway, one nice thing is when i get to letting players customize key maps, I won’t have to change any code.

Learning all the codes for the buttons on your keyboard would probably be hard, if you wanted to know “G” you would have to look it up, with GameKeys you know its GKEY. It just makes this a bit easier.

In the newer 2.5 builds you don’t need keyboard and mouse sensors anymore, there has been some slight changes to naming in the API as well giving access to the keyboard and mouse.

import bge # parent GE module

# bge.logic is the equivalent of GameLogic
# bge.logic.keyboard gives access to the keyboard
keyboard = bge.logic.keyboard 

for keycode, status in
    # holds all mouse and keyboard keycodes
    if keycode == and status == bge.logic.KX_INPUT_JUST_ACTIVATED:

Check the new docs if you need to look something up, the new docs are great.

In regards to actually using the, I would make my own list of active keys and then check those:

x = 0.0
y = 0.0

if in active_keys:
    y += 9.0
elif in active_keys:
    y -= 9.0
if in active_keys:
    x -= 9.0
if bge.evemts.DKEY in active_keys:
    x += 9.0

if x and y:
    x *= 0.7071
    y *= 0.7071

player.applyForce([x,y,0], 1)