0
$\begingroup$

I've been trying to figure out a way to make a script that determines if a joystick button has been pressed, being held down or released. I managed to make it work with a keyboard button and then tried to convert my script to work with the joystick.

Here's the keyboard script (works by pressing H)

import bge

cont = bge.logic.getCurrentController()

keyboardsens = cont.sensors["Keyboard"]

bStatus0 = keyboardsens.getKeyStatus(bge.events.HKEY)

if bStatus0 == bge.logic.KX_INPUT_JUST_ACTIVATED:
    print("just pressed")

if bStatus0 == bge.logic.KX_INPUT_ACTIVE:
    print("is pressed")

if bStatus0 == bge.logic.KX_INPUT_JUST_RELEASED:
    print("just released")

And here's the console output:

enter image description here

Here's the joystick script (works by pressing button 0, or in my case the B button):

import bge

cont = bge.logic.getCurrentController()

joysticksens = cont.sensors["Joystick"]

bStatus0 = joysticksens.getButtonStatus(0)

if bStatus0 == bge.logic.KX_INPUT_JUST_ACTIVATED:
    print("just pressed")

if bStatus0 == bge.logic.KX_INPUT_ACTIVE:
    print("is pressed")

if bStatus0 == bge.logic.KX_INPUT_JUST_RELEASED:
    print("just released")

And here's this ones output:

enter image description here

See the problem I'm having here is that with the joystick, I don't get the initial "just pressed" or "just released" messages, just the in between "is pressed" message. How do I fix this?

$\endgroup$
2
$\begingroup$

You're checking the button status, but the constants used are for sensor statuses. For those checkings, use sensor.status instead of sensor.getButtonStatus. Example:

if joysticksens.status == bge.logic.KX_INPUT_JUST_ACTIVATED:
    print("just pressed")

Remember that sensor.getButtonStatus returns only True or False, and equals to the expression:

BUTTON in bge.logic.joystics[0].activeButtons

Use the sensor.getButtonStatus along with sensor.status to check if a given button is pressed and which status of sensor at the same time.

Below, your file reworked using the method I mentioned, and few additions:

  • Optimized by using Module mode instead of Script mode in controller
  • Keyboard status checking without using the Keyboard sensor.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.