how to add Tap in keyboard[bge.events.WKEY]



import bge

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


Motion = cont.actuators["Motion"]


keyboard = bge.logic.keyboard.events


W = keyboard[bge.events.WKEY]


if W > 0:
    cont.activate(Motion)
else:
    cont.deactivate(Motion)

in this script how to make the W key active only one time even if we keep holding it, like using Tap in logic bricks?
thank you

if W==1:

I think in upbge

I will check


you can ditch the motion actuators with the functions .applyMovement((x, y, z), Local) and .applyRotation((x, y, z), Local)


import bge


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


#Motion = cont.actuators["Motion"]

keyboard = bge.logic.keyboard.events
inactive = bge.logic.KX_INPUT_NONE  # aka 0
tap = bge.logic.KX_INPUT_JUST_ACTIVATED  # aka 1
active = bge.logic.KX_INPUT_ACTIVE  # aka 2
released = bge.logic.KX_INPUT_JUST_RELEASED  # aka 3

W = keyboard[bge.events.WKEY]


if W == tap:
#    cont.activate(Motion)
    own.applyMovement((0, 0.1, 0), True)  # True for Local, False for World
    own.applyRotation((0, 0, 0.1), True)  # Rotation
#else:
#    cont.deactivate(Motion)

https://docs.blender.org/api/blender_python_api_current/bge.types.KX_GameObject.html#bge.types.KX_GameObject.applyMovement

im using the motion actuators just to test the script, thank you for your code

You are rebuilding the keyboard sensor in Python?

What is wrong using the sensor as it is?

To answer your question - the key event has four status:

  • bge.logic.KX_INPUT_NONE
  • bge.logic.KX_INPUT_JUST_ACTIVATED
  • bge.logic.KX_INPUT_ACTIVE
  • bge.logic.KX_INPUT_JUST_RELEASED

Try this in your file (always sensor true level triggering enabled):


import bge

keyboard = bge.logic.keyboard

w_key_status = keyboard.events[bge.events.WKEY]


if w_key_status == bge.logic.KX_INPUT_NONE:
    print("not pressed")
elif w_key_status == bge.logic.KX_INPUT_JUST_ACTIVATED:
    print("just pressed")
elif w_key_status == bge.logic.KX_INPUT_ACTIVE:
    print("hold")
elif w_key_status == bge.logic.KX_INPUT_JUST_RELEASED:
    print("just released")
else:
    print("You should never see this message. There are no other status")


When you read the console output carefully you can guess what you need to check. If not let us know.

  • I somehow passed by here … and did see what I need, too. Thank you.