Python keypress - Solved =]

Hey,
This is a simple question about keypress in pyhton, but strangely I can’t find it anywhere. When I usually need a keypress in python I do it like this:

forward = cont.getSensor(“Up”)

if forward.isPositive():

How can I make multiple keys, (so for example key 1, 2, a, b and c) using 1 sensor?

Thx =]

Just activate the “All Keys” button on the sensor. Then use the sensor .getKeyStatus(key) attribute from the sensor:
http://www.blender.org/documentation/249PythonDoc/GE/GameTypes.SCA_KeyboardSensor-class.html

I’m sorry i’m a real newb in python. I don’t understand how to do this :stuck_out_tongue:
Can you plz help?

Say your sensor name is “Keys”, then you just do it like:

import GameKeys

keys = cont.getSensor(“Keys”)

if keys.getKeyStatus(“UPARROWKEY”): #go forward
if keys.getKeyStatus(“DOWNARROWKEY”): #go backwards

etc… to know what each key code is, you can check:
http://www.blender.org/documentation/249PythonDoc/GE/GameKeys-module.html

Damn I can’t figure it out =[
What I have now is this:
My sensor is called: “key” without quotes

cont = GameLogic.getCurrentController()
own = cont.getOwner()

import GameKeys

keys = cont.getSensor(“key”)

speed = 0.1
rspeed = 0.02
walk = 0
turn = 0

if keys.getKeyStatus(“WKEY”):
walk = speed

motion.setDLoc(0, walk, 0, 1)
motion.setDRot(0, 0, turn, 1)
GameLogic.addActiveActuator(motion, 1)

Plz help.
Ps: I started python today so I’m really a noob.

the walk = speed line needs to be indented.

What do you mean? My original script was this which works:

cont = GameLogic.getCurrentController()
own = cont.getOwner()

forward = cont.getSensor(“Up”)
backward = cont.getSensor(“Down”)
turnleft = cont.getSensor(“Left”)
turnright = cont.getSensor(“Right”)

motion = cont.getActuator(“motion”)

speed = 0.1
rspeed = 0.02
walk = 0
turn = 0

if forward.isPositive():
walk = speed
if backward.isPositive():
walk = -speed
if turnleft.isPositive():
turn = rspeed
if turnright.isPositive():
turn = -rspeed

motion.setDLoc(0, walk, 0, 1)
motion.setDRot(0, 0, turn, 1)
GameLogic.addActiveActuator(motion, 1)

hi gamemaster,

Your code contained a couple of errors.

1st error: The code to get the actuator that you are using to move and rotate your game object was missing.

2nd error: The code to get the key code (from the GameKeys module) for the WKEY was missing.

3rd error: getKeyStatus returns an integer. (link to getKeyStatus method)
0 = not active. 1 = just pressed. 2 = active. 3 = released

Try the code below.

# get the current controller
cont = GameLogic.getCurrentController()

# get owner
own = cont.getOwner()

# import GameKeys Module
import GameKeys

# get sensor named key
keys = cont.getSensor("key")

# get actuator named motion
motion = cont.getActuator("motion")

# set variables
speed = 0.1
rspeed = 0.02
walk = 0.0
turn = 0.0

# get Game key code for W key
wKey = GameKeys.WKEY

# if get key status is 1 (just pressed)
if keys.getKeyStatus(wKey) == 1:
    walk = speed

# move on local y axis
motion.setDLoc(0, walk, 0, True)

# rotate on world z axis
motion.setDRot(0, 0, turn, False)

# use actuator
GameLogic.addActiveActuator(motion, True) 

Clark

Line 27
AttributeError: getKeyStatus

hi gamemaster,

Here’s a blend.

Clark

Attachments

keysStatus.blend (153 KB)

Quess there is something wrong with my python. Still get the error with the getKeyStatus.

Re-downloaded blender. And it works. Thanks so much for your help. :slight_smile: