Search code examples
pythoncontrollerxbox

Python xbox controller inputs. How to change the message send when a button is pressed


I am currently making a game and I wanted to add controller support.

Here is my current code:

from inputs import get_gamepad
while 1:
    events = get_gamepad()
    for event in events:
        print(event.ev_type, event.code, event.state)

I am trying to make it so, for say when I press the A button it sends the message:

"a" 

instead of:

Key BTN_SOUTH 1
Sync SYN_REPORT 0
Key BTN_SOUTH 0
Sync SYN_REPORT 0

Thanks for taking the time to read this


Solution

  • You can use the on_press and on_release methods to specify the message that should be sent when a button is pressed or released.

    import inputs
    
    # create a controller object
    controller = inputs.devices.gamepads[0]
    
    @controller.on_press('A')
    def on_press_a(button):
        print("A button pressed")
    
    @controller.on_release('A')
    def on_release_a(button):
        print("A button released")
    

    You can do the same for any other button on the controller