title: Pygame Get Value of Button
body: <p>I've got this problem with Pygame that I cannot seem to find a solution too. I'm trying to use a PS4 controller to control an RC car. I've got connecting to the controller to the program and seeing how many buttons it has, but I'm unable to get the value of a pushed button in a useable form. </p>
  
  <p>Basically, I want to be able to get all of the values of the buttons on the controller. When a button is pressed, the program updates the value of that button and allows me to execute code based on the value of that button.</p>
  
  <p>Here's what I've got so far:</p>
  
  <pre><code>import os
  import pprint
  import pygame
  from time import sleep
  
  class PS4Controller(object):
      """Class representing the PS4 controller. Pretty straightforward functionality."""
  
  controller = None
  axis_data = None
  button_data = None
  hat_data = None
  
  def init(self):
      """Initialize the joystick components"""
  
      pygame.init()
      pygame.joystick.init()
      self.controller = pygame.joystick.Joystick(0)
      self.controller.init()
  
  def listen(self):
      """Listen for events to happen"""
  
      if not self.axis_data:
          self.axis_data = {}
  
      if not self.button_data:
          self.button_data = {}
          for i in range(self.controller.get_numbuttons()):
              self.button_data[i] = False
  
      if not self.hat_data:
          self.hat_data = {}
          for i in range(self.controller.get_numhats()):
              self.hat_data[i] = (0, 0)
  
      while True:
          for event in pygame.event.get():
              if event.type == pygame.JOYAXISMOTION:
                  self.axis_data[event.axis] = round(event.value,2)
                  #print(self.axis_data)
              elif event.type == pygame.JOYBUTTONDOWN:
                  self.button_data[event.button] = True
                  print (self.button_data)
                  print("\n")
              elif event.type == pygame.JOYBUTTONUP:
                  self.button_data[event.button] = False
                  #print(self.button_data)
              elif event.type == pygame.JOYHATMOTION:
                  self.hat_data[event.hat] = event.value
                  #print(self.hat_data)
  
              # Insert your code on what you would like to happen for each event here!
              # In the current setup, I have the state simply printing out to the screen.
  
  
  
              #os.system('clear')
              #pprint.pprint(self.button_data)
              #pprint.pprint(self.axis_data)
              #pprint.pprint(self.hat_data)
  
  
  if __name__ == "__main__":
      ps4 = PS4Controller()
      ps4.init()
      ps4.listen()
  </code></pre>
  
  <p>Here's the output when I press the "X" button on the controller:</p>
  
  <pre><code>{0: False, 1: True, 2: False, 3: False, 4: False, 5: False, 6: False, 7: False, 8: False, 9: Falselse, 12: False, 13: False}
  </code></pre>
  
  <p>This is good, but I'm unable to use the data from this output when it's in this format.</p>
  
  <p>Right now, if the code is run, it will output which button was pressed, but the button_data is not in a usable state. How can I run, for example:</p>
  
  <pre><code>if button3 == True:
        #execute code
   esif button4 == True:
       #execute some different code
  </code></pre>
  
  <p>Thanks!</p>