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()