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 keyboardsensor.events 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 keyboardsensor.events method, and it works pretty good, so what is the point of the GameKeys module besides converting keycodes into strings and vise-versa?
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 keyboard.events:
# bge.events holds all mouse and keyboard keycodes
if keycode == bge.events.AKEY and status == bge.logic.KX_INPUT_JUST_ACTIVATED:
print(1)
In regards to actually using the keyboard.events, I would make my own list of active keys and then check those:
x = 0.0
y = 0.0
if bge.events.WKEY in active_keys:
y += 9.0
elif bge.events.SKEY in active_keys:
y -= 9.0
if bge.events.AKEY 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)