HELP Making a Keyboard Sensor with Python

The example code in the API doesn’t work.
From: https://www.blender.org/api/blender_python_api_2_77_1/bge.events.html#module-bge.events


# 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 :frowning:

This sounds like your design is not sufficient.

Within the BGE you have to use the logic bricks. Even if there are just two of them. You do not have to use Python, you can use it.

To analyze what your code is doing (or not doing) I suggest to add some print statements at strategical places.

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.

import bge

cont = bge.logic.getCurrentController()
own = cont.owner
    
keyboard = bge.logic.keyboard
   
wKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.WKEY]
aKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.AKEY]
sKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SKEY]
dKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DKEY]




if wKey:
    own.applyMovement([0, 1, 0], True)
    
if sKey:
    own.applyMovement([0, -1, 0], True)
    
if aKey:
    own.applyMovement([-1, 0, 0], True)
    
if dKey:
    own.applyMovement([1, 0, 0], True)
1 Like

well the code seems to work for me:


# 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

Thanks guys, this helped a lot.