Action while holding Button

Hy,

for the TB Joystick (tbJoystick.py) steering i have the problem when i press a button for e.g. slower, fasten or spinning, the movement continues after i release the button until i press a new button or move the joystick. But this is not what i want. I tried a lot but got no solution, i am also not high experienced with programming so i am very happy of help of you.

The program is almost in the initial state, i just added the lines with definition shown in picture

Images: 
piborg's picture

I think it is fairly simple to fix :)

Towards the top of the loop there is a section which checks if the pygame event needs to be processed:

            if event.type == pygame.QUIT:
                # User exit
                running = False
            elif event.type == pygame.JOYBUTTONDOWN:
                # A button on the joystick just got pushed down
                hadEvent = True                    
            elif event.type == pygame.JOYAXISMOTION:
                # A joystick has been moved
                hadEvent = True

This section decides the event is of interest if it is caused by a joystick moving or a button being pressed.

What you need to do is also process events where a button is released as well. This means adding JOYBUTTONUP to this section like so:

            if event.type == pygame.QUIT:
                # User exit
                running = False
            elif event.type == pygame.JOYBUTTONDOWN:
                # A button on the joystick just got pushed down
                hadEvent = True                    
            elif event.type == pygame.JOYBUTTONUP:
                # A button on the joystick just got released
                hadEvent = True                    
            elif event.type == pygame.JOYAXISMOTION:
                # A joystick has been moved
                hadEvent = True

Once changed the script should recalculate the motor output when you let go of the button, causing it to stop as expected.

Hy,

a late response.
Yes, it was that easy.

Thank you guys.

Subscribe to Comments for "Action while holding Button"