how to print after button is up?

Viewed 47

using pygame, when i execute the program, all i want to do is to keep printing "mode 1". If i press button number 7 on the joystick, i want it to print "mode 2" only after button 7 has been UP (meaning pressed then released). If i press button 7 again, i want it to print "mode 1" again. I tried using pygame.JOYBUTTONDOWN and pygame.JOYBUTTONUP but it just doesnt work and the best code i can come up with is the following: Result: it prints "mode 1".....then it prints "mode 2" only if i keep holding button 7 :( if i let go of button 7, it just goes back to printing "mode 1"

import sys

import pygame
import time
from pygame.locals import *
pygame.init()

global data_of_axis 

def main():

    global data_of_axis
    gui_screen = pygame.display.set_mode((500, 700))
    pygame.display.set_caption("Joystick example")

    joysticks = {}
   

    while True:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True  # Flag that we are done so we exit this loop.

                # Handle hotplugging
                if event.type == pygame.JOYDEVICEADDED:
                    # This event will be generated when the program starts for every
                    # joystick, filling up the list without needing to create them manually.
                    joy = pygame.joystick.Joystick(event.device_index)
                    joysticks[joy.get_instance_id()] = joy
                    #print("Joystick {} connencted".format(joy.get_instance_id()))

                if event.type == pygame.JOYDEVICEREMOVED:
                    del joysticks[event.instance_id]
                    #print("Joystick {} disconnected".format(event.instance_id))

        for joystick in joysticks.values():
            
            

            butt7Down = joystick.get_button(7)          
            
            if butt7Down:              

                print("mode 2") 

            
            else:

                print("mode 1")
            
            
            time.sleep(0.1)
            
                        
                        
        pygame.display.update()
        


if __name__ == "__main__":
    main()
    # If you forget this line, the program will 'hang'
    # on exit if running from IDLE.
    pygame.quit()
1 Answers

One way to do this is to watch for a Joystick button event, and then re-read the status of all joystick buttons. Whenever the new set of states is read, make a copy of the set, to notice changes between "before" and "now".

This allows the code to simply check if the previous state was pressed, but now it isn't, and then print the appropriate output string.

# Create a list of "released" button states
button_states = [ False for i in range( joystick.get_numbuttons() ) ]
prev_states = button_states.copy()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.JOYBUTTONDOWN or \
             event.type == pygame.JOYBUTTONUP:
             
            prev_states   = button_states.copy()   # make a copy of the previous states
            # Whenever we see a joystick event, re-read the status of all buttons
            button_states = [ joystick.get_button( i ) for i in range( joystick.get_numbuttons() ) ]
         
  # [...]

  # If there's enough button states
  if len( button_states ) > 7 and \
     len( prev_states ) > 7:

     # If the button was pushed, but now is released
     if prev_states[7] == True and button_states[7] == False:
         print( "mode2" )
     else:
         print( "mode1" )
Related