GameKeys help

Hey guys, I have this script using GameKeys and I’m pretty satisfied with it, but if you press and hold down W and then press S, the character will keep moving backwards until you release W. Can you help me out please?

#Necessary stuff
import GameLogic as g
import GameKeys as gk

c = g.getCurrentController()

#This is my Keyboard sensor, with “All Keys” selected
Keys = c.sensors[“Keyboard”]

#The motion actuator
Move = c.actuators[“Move”]

#Define Variables
FB = 0.0
LR = 0.0
key = “”

#If a button is pressed, it will get the pressed key
if Keys.positive:
key = gk.EventToString(Keys.getPressedKeys()[0][0])

#Assigns value due to the key pressed
if key == “WKEY”:
FB = .1
if key == “SKEY”:
FB = -.1
if key == “AKEY”:
LR = -.1
if key == “DKEY”:
LR = .1

#Assign values to the Actuator and activate it
Move.dLoc = (LR, FB, 0.0)

Hi there!

Sounds like you’re trying to avoid using more than 1 sensor - I don’t think you can do that for this instance as the sensor for ‘any key’ is re-triggered when another key is pressed, and because it is looking for only one key - it will only report the last key pressed which triggered the event.

you can do almost exactly what you were doing by adding 3 more sensors, see the attached blend file and let me know what you think - it does exactly what you are trying to do :slight_smile:


KeyMove.blend (131 KB)

Your script is checking just one key out of the list of pressed keys. All other pressed keys will be ignored.

key = gk.EventToString(Keys.getPressedKeys()[0][0])

you can make a loop over all keys:

for event in
    if key==GameKeys.WKEY:

or check the status directly:

if GameKeys.getKeyStatus(GameKeys.WKEY)==GameKeys.KX_INPUT_JUST_ACTIVATED \
or GameKeys.getKeyStatus(GameKeys.WKEY)==GameKeys.KX_INPUT_ACTIVE:
    #W-key is pressed