# Do the all keys thing
import bge
co = bge.logic.getCurrentController()
# 'Keyboard' is a keyboard sensor
sensor = co.sensors["Keyboard"]
for key,status in sensor.events:
# key[0] == bge.events.keycode, key[1] = status
if status == bge.logic.KX_INPUT_JUST_ACTIVATED:
if key == bge.events.WKEY:
# Activate Forward!
if key == bge.events.SKEY:
# Activate Backward!
if key == bge.events.AKEY:
# Activate Left!
if key == bge.events.DKEY:
# Activate Right!
I kinda get the jist that you have to create a blank Keyboard sensor node on the Game Logic panel and be sure it’s called “Keyboard,” but I still can’t get it to work.
I would really like to get this working because relying on the Game Logic nodes for all of my keyboard inputs is becoming the bane of my existence. So many things break when I’m not sticking exclusively with Python scripting. I’ve googled this issue and can’t find a solution anywhere. I really need help. This is a roadblock in my progress right now
This is what I use for my movement scripts, and it always works for me, pure python, no additional logic bricks, just an always sensors and a python controller.
# Do the all keys thing
import bge
co = bge.logic.getCurrentController()
# 'Keyboard' is a keyboard sensor
sensor = co.sensors["Keyboard"]
owner = co.owner #dont know if you had this in your code
for key,status in sensor.events:
# key[0] == bge.events.keycode, key[1] = status
if status == bge.logic.KX_INPUT_ACTIVE: #you had KX_INPUT_JUST_ACTIVATED - so the cube would only move once
if key == bge.events.WKEY: #also make sure the keyboard sensor has true level triggering
# Activate Forward!
owner.applyMovement((0,1,0), True)
if key == bge.events.SKEY:
# Activate Backward!
owner.applyMovement((0,-1,0), True)
if key == bge.events.AKEY:
# Activate Left!
owner.applyMovement((-1,0,0), True)
if key == bge.events.DKEY:
# Activate Right!
owner.applyMovement((1,0,0), True)
edit: also: keyboard sensor has to have “All Keys” checked