Page 1 of 1

"keyboard.key" always returns 0

Posted: Wed May 08, 2013 8:02 am
by dschiller
Hi all,

here is my code:

Code: Select all

import bge

cont = bge.logic.getCurrentController()
owner = cont.owner

mouse = cont.sensors["Mouse2"]
keyboard = cont.sensors["Keyboard2"]
width = bge.render.getWindowWidth()
width = width/2

pos = mouse.position

posX = width-pos[0]
posX = posX*0.02*-1
if keyboard.getKeyStatus(129) ==1 or keyboard.getKeyStatus(129) == 2:
    owner.localPosition.x = posX
The code

Code: Select all

always returns "0" no matter which key i press. Anyone an Idea ?


Re: "keyboard.key" always returns 0

Posted: Fri May 24, 2013 8:56 am
by dschiller
Muffy / wrote:The API states that sensor.key simply returns the keycode the sensor is looking for. Since the "key" field in your sensor is nothing, it is returning zero.

To get a list of the pressed keys, use instead. This handles multiple keys pressed at once. For every event in the list index[0] is the keycode and index[1] is the key status.

Try changing your if-else statement at the end of the script for this :

for event in :
if event[0] == 129 :
owner.localPosition.x = posX
else :