Keyboard

In this lesson we'll be making use of our keyboard using python. There are different ways of checking the keyboard input, and we'll take a look at two of them.

Method 1

We'll import the game engine module as usual and we need bge.logic.keyboard which is wrapped in SCA_PythonKeyboard class.
This attribute has a dictionary which is .activeInputs which holds the active input of the keyboard.
So our code will be looking like:

try:
    import bge
except:
    import Range as bge
def main(): # main function
    cont = bge.logic.getCurrentController() # the script
    own = cont.owner # scripts object
    keysdown = bge.logic.keyboard.activeInputs # keyboard list

Now in order to check if this is working, we add a cube and leave the name as Cube and make it move.
We'll check if the keyboard has any active keys in the .activeInputs like this

x = 0.0 # set x
if 45 in keysdown: # look for W key in list, 45 = W key
    y = 10.0 # set y

Checking if the key W is in that dictionary so for remaining all A, S and D keys we'll do the following.

x = 0.0 # set x
if 45 in keysdown: # look for W key in list, 45 = W key
    y = 10.0 # set y
elif 41 in keysdown and not 45 in keysdown:
    y = -10
else:y = 0.0 # no W key in list
z = 0.0 # set z

if 23 in keysdown and not 26 in keysdown:
    zt = 100.0/4
elif 26 in keysdown and not 23 in keysdown:
    zt = -100.0/4
else:zt = 0.0

If you are bit confused how 45 means W you can look at the documentation, and here are the keycodes for the four keys.

Now to check if it's working we print keydown. Open up the system console by going to Window -> Toggle System Console.
By the way, this is how our code looks.

try:
    import bge
except:
    import Range as bge
def main(): # the start
    cont = bge.logic.getCurrentController() # the script
    own = cont.owner # scripts object
    keysdown = bge.logic.keyboard.activeInputs # keyboard list
    x = 0.0 # set x
    if 45 in keysdown: # look for W key in list, 45 = W key
        y = 10.0 # set y
    elif 41 in keysdown and not 45 in keysdown:
    	y = -10.0
    else:y = 0.0 # no W key in list
    z = 0.0 # set z
    
    if 23 in keysdown and not 26 in keysdown:
        zt = 100.0/4
    elif 26 in keysdown and not 23 in keysdown:
        zt = -100.0/4
    else:zt = 0.0

    print(keysdown)

And this is how the logic bricks setup look.

Logic Bricks Setup
Logic Bricks Setup

Now when we run the code it should look something like below.

Logic Bricks Setup
User input being checked

For the cube to move just add these two lines replacing print("keysdown").

own.applyForce((x, y, z), True) # True = local
own.applyTorque((0.0, 0.0, zt), True) # True = local

And finally that's it, we have a working keyboard setup written in python.
Download the file.

Method 2

Now we'll be doing the keyboard input by checking the bge.logic.keyboard.events dictionary.
Make import the game engine module, create a function and name it as main or whatever you like. I think naming it as main is good, so we'll go with it and we this time create a list for movement and rotation.
Oh we also need to define what our bge.logic.keyboard is going to be, we'll name it as keyboard. We need a speed variable which is not going to be changed, so it will be constant.

try:
    import bge
except:
    import Range as bge

cont = bge.logic.getCurrentController()

def main(cont):
    own = cont.owner
    keyboard = bge.logic.keyboard # Get the keyboard events

    movement = [0, 0, 0] # elements are considered as X, Y, Z
    rotation = [0, 0, 0]

    SPEED = 10 # Constant value

Declaring this in uppercase doesn't prevent us from changing the value, its just meant to be a "pythonic" way.
Now to the logic part, we check for user input like this.

if bge.logic.KX_INPUT_ACTIVE in keyboard.inputs[bge.events.WKEY].status:
    movement[1] += speed # change Y axis value of movement
if bge.logic.KX_INPUT_ACTIVE in keyboard.inputs[bge.events.SKEY].status:
    movement[1] -= speed
if bge.logic.KX_INPUT_ACTIVE in keyboard.inputs[bge.events.AKEY].status:
    rotation[2] += speed # change Z axis value of rotation
if bge.logic.KX_INPUT_ACTIVE in keyboard.inputs[bge.events.DKEY].status:
    rotation[2] -= speed

In this code we're basically checking for an active input in the list keyboard.inputs[bge.events.<SOMEKEY>].status which can either contain bge.logic.KX_INPUT_NONE or bge.logic.KX_INPUT_ACTIVE. If so move our object.
And finally for movement we do the following.

own.applyForce(movement, True)
own.applyTorque(rotation, True)

.applyForce() and .applyTorque() accepts list or tuple, as our movement and rotation is already a list we'll pass it like that.
Here goes the full code.

try:
    import bge
except:
    import Range as bge

cont = bge.logic.getCurrentController()

def main(cont):
    own = cont.owner
    keyboard = bge.logic.keyboard # Get the keyboard events

    movement = [0, 0, 0]
    rotation = [0, 0, 0]

    SPEED = 10

    # Check for user input
    if bge.logic.KX_INPUT_ACTIVE in keyboard.inputs[bge.events.WKEY].status:
        movement[1] += speed
    if bge.logic.KX_INPUT_ACTIVE in keyboard.inputs[bge.events.SKEY].status:
        movement[1] -= speed
    if bge.logic.KX_INPUT_ACTIVE in keyboard.inputs[bge.events.AKEY].status:
        rotation[2] += speed
    if bge.logic.KX_INPUT_ACTIVE in keyboard.inputs[bge.events.DKEY].status:
        rotation[2] -= speed

    own.applyForce(movement, True)
    own.applyTorque(rotation, True)

Download the project file here, have a nice day!