Python Key not triggering

So I have a camera for my FPS and I am writing a script for it to look at and handle the different items and weapons on the map to alter properties appropriately.

I am trying to write it so that if I press R while looking at something with a certain property it will change my “left hand” property to reflect I am not holding something.

The layout is:


some stuffs...

sens = cont.sensors['mouseSelect']

keyboard = bge.logic.keyboard
equip = bge.logic.KX_INPUT_JUST_ACTIVATED == keyboard.events[bge.events.RKEY]

some more stuffs....

if equip:
      do the rest of it

The problem is, the equip never triggers true, and never enters that if. I have no console errors with this setup, and debug print statements confirm it reaches right to that point, then skips the if.

I also tested it by instead of if equip:, it is if sens.positive:, and this works, confirming that the script works without the equip key trigger attempt.

So any ideas what I am failing to see here?

If I also wanted to make it an and, in C I would say if(equip && sens), how would I make such an && statement in python? Forgive my noobiness on this :o.

I suggest you use an keyboard sensor to directly sense the key you want. You can check the sensor status with.positive without to worry what key is sensed and if it is a leyboard sensor at all. This is simple and it separates the operations you want to perform from the events you want to sense.

Okay yes, with the sensor it works perfectly.

Could anybody please elaborate on my second question of how to do an and in blender python?

welll in python you could run a test using your code.


keyboard = bge.logic.keyboard
equip = bge.logic.KX_INPUT_JUST_ACTIVATED == keyboard.events[bge.events.RKEY]


if equip.positive:
      print("Hello World")


You should see Hello World print once to the console, per time you hit R

I suggest to read the Python documentation, or follow some of the online Python lessons.

In python the logical operation AND is called “and”:


if conditionA and conditionB:

A heads up, this code won’t work. The positive attribute belongs to the Logic Brick sensors.