1

I'm trying to make a small Python script that executes another function (opens steam) when I press 2 buttons at the same time.

Here's the problem: the buttons 6 and 7 aren't working. When I print the values of them they return 0 even if I'm pressing the keys on my controller.

Does anyone know what's wrong here:

import os
import pygame

pygame.init()

#j = pygame.joystick.Joystick(0)
#j.init()
clock = pygame.time.Clock()
 

discon = False
def check_pad():
    global discon
    pygame.joystick.quit()
    pygame.joystick.init()
    joystick_count = pygame.joystick.get_count()

 

    
for i in range(joystick_count):
    joystick = pygame.joystick.Joystick(i)        
    joystick.init()
    istarted = pygame.joystick.get_init()

if not joystick_count: 
    if not discon:
       print ("Controller Disconnected")
       discon = True
    clock.tick(20)  
    check_pad()
else:
    print("Controller Connected")
    print(joystick.get_button(6))
    print(joystick.get_button(7))
    print(istarted)
    print(pygame.joystick.get_count())
    discon = False

    if(joystick.get_button(6) and joystick.get_button(7)):
        initiate_steam()

while True:
    check_pad()
Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • 1
    How are your sure they are number 6 and 7? – MegaIng Jul 15 '21 at 11:14
  • I saw this documentation: https://www.pygame.org/docs/ref/joystick.html saying that the back and start buttons on my Xbox controller, are number 6 and 7. These are the buttons I want – Super_X-dev Jul 15 '21 at 11:26
  • 1
    Fair enough, didn't know they list that kind of stuff. But I only now realize: pygame isn't designed to be use without a display, so it might not be reliable: https://stackoverflow.com/questions/62003575/pygame-not-returning-joystick-axis-movement-without-display – MegaIng Jul 15 '21 at 11:34
  • Uhm, interesting. I´ll retink this and try another way of doing it. Yestarday when I started this it worked, except if the controller got disconnected, then it would stop working even if it was connected again, which wasn´t ideal for the type of thing I want to do. Thank You anyways – Super_X-dev Jul 15 '21 at 13:10
  • I don't know if the indentation is correct in the question, but if you run the code as it is displayed, the state of the two buttons will be evaluated just once. You need to indent the lines from `for i in range(joystick_count):` to the `while` loop to get the state of the buttons at any moment. – D_00 Jul 20 '21 at 12:39

0 Answers0