"keyboard.key" always returns 0

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
dschiller
Posts: 0
Joined: Wed May 08, 2013 7:43 am
Location: Germany

"keyboard.key" always returns 0

Post by dschiller » Wed May 08, 2013 8:02 am

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
else:
    print(keyboard.key)
The code

Code: Select all

print(keyboard.key)
always returns "0" no matter which key i press. Anyone an Idea ?

Image
Cheers
Dirk

dschiller
Posts: 0
Joined: Wed May 08, 2013 7:43 am
Location: Germany

Re: "keyboard.key" always returns 0

Post by dschiller » Fri May 24, 2013 8:56 am

Muffy / Blenderartists.org 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 sensor.events 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 keyboard.events :
if event[0] == 129 :
owner.localPosition.x = posX
else :
print(event[0])
Cheers
Dirk

Post Reply