Action while holding Button
Forums:
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
- Log in to post comments
 
     
      






piborg
Sat, 11/09/2019 - 20:46
Permalink
Stop when button is released
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 = TrueThis 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
JOYBUTTONUPto 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 = TrueOnce changed the script should recalculate the motor output when you let go of the button, causing it to stop as expected.
Canaoz
Sat, 11/23/2019 - 10:40
Permalink
Hy,
Hy,
a late response.
Yes, it was that easy.
Thank you guys.