You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So it's probably my lack of expertise with async programming speaking here but I could not figure out as to how to detect any button combinations being made on the controller, any way to do so or to implement some the filters to do the same?
The text was updated successfully, but these errors were encountered:
Unfortunately, XInput-Python does not offer this as a built-in feature, but it's still fairly simple to implement.
I've assembled two examples of how this can be done using the EventHandler class:
importXInputclassCustomHandler(XInput.EventHandler):
A_AND_B_BUTTONS=XInput.BUTTON_A|XInput.BUTTON_BMASK=0xFFFFFFdef__init__(self, *controllers):
super().__init__(*controllers, filter=CustomHandler.A_AND_B_BUTTONS)
self.buttons_pressed=0defprocess_button_event(self, event):
ifevent.type==XInput.EVENT_BUTTON_PRESSED:
self.buttons_pressed|=event.button_idelse:
self.buttons_pressed&= (CustomHandler.MASK^event.button_id)
ifself.buttons_pressed==CustomHandler.A_AND_B_BUTTONS:
print("CustomHandler detected: A and B have been pressed.")
defprocess_stick_event(self, event):
passdefprocess_trigger_event(self, event):
passdefprocess_connection_event(self, event):
passclassAlternativeCustomHandler(XInput.EventHandler):
def__init__(self, *controllers):
super().__init__(*controllers, filter=XInput.BUTTON_A|XInput.BUTTON_B)
self.buttons_pressed= {"A" : False, "B": False}
defprocess_button_event(self, event):
ifevent.type==XInput.EVENT_BUTTON_PRESSED:
self.buttons_pressed[event.button] =Trueelse:
self.buttons_pressed[event.button] =Falseifall(self.buttons_pressed.values()):
print("AlternativeCustomHandler detected: A and B have been pressed.")
defprocess_stick_event(self, event):
passdefprocess_trigger_event(self, event):
passdefprocess_connection_event(self, event):
passmain_handler=CustomHandler(0, 1, 2, 3)
alt_handler=AlternativeCustomHandler(0, 1, 2, 3)
thread=XInput.GamepadThread(main_handler)
thread.add_event_handler(alt_handler)
Each of the event handlers filter out any buttons, except for the A and B buttons, which in this example are the ones that shall be pressed together.
The event handlers then keep track of the state of both buttons in some way and print a message on the console when both buttons are currently being pressed.
Admittedly, the filter/handler system isn't very great. Maybe I'll come up with a better solution some day.
So it's probably my lack of expertise with async programming speaking here but I could not figure out as to how to detect any button combinations being made on the controller, any way to do so or to implement some the filters to do the same?
The text was updated successfully, but these errors were encountered: