GameKeys and getPressedKeys

Hi, my script is that (to get keypressed)


import GameKeys as gk
import GameLoic as g

cont = g.getCurrentController()
keys = cont.getSensor('Key')
keylist = keys.getPressedKeys()

#print gk.UKEY
if keys.isPositive():
    for key in keylist:
        print key[0]
        if key[0] == 146:
            print "u"

the script works but i allways got this message in consol : :frowning:


PYTHON SCRIPT ERROR:
Traceback (most recent call last):
  File "Variables.py", line 11, in ?
TypeError: iteration over non-sequence
146
u

the 2 last lines prove that’s working !

Did you look over the GE docs on GameKeys:

http://www.blender.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/GameKeys-module.html

At the bottom there is an example script that shows the usage.

yes of course, i start with this script…

i would use


import GameLoic as g

cont = g.getCurrentController()
keys = cont.getSensor('Key')
keylist = keys.getPressedKeys()

if keylist:
   if key[0][0] == 146:
      print "u"

and the iteration over non sequence means youre trying to loop through sometyhing that is not a list because you have to chack and make sure there is a list before you loop it

As for me, I would use this script:

import GameLogic as g
cont = g.getCurrentController()
keys = cont.getSensor('Key')
keylist = keys.getPressedKeys()
 
if keylist:
   if key[0] == [146,1]:
      print "u"

That way, it only prints one “u” per keypress ([146,1] means the key is being pressed this frame, [146,2] means the key is held this frame, [146,3] means the key is released this frame).

wait i didnt write mine right… use toomai’s

ohh ok, thx :slight_smile:


import GameLogic as g
cont = g.getCurrentController()
keys = cont.getSensor('Key')
keylist = keys.getPressedKeys()
 
if keylist:
   if <b>keylist[0]</b> == [146,1]:
      print "up Key" 

works