importasynciofromAsynchronousPS4ControllerimportControllercontroller=Controller(path="/dev/input/js0")# you can start listening before controller is paired, as long as you pair it within the timeout windowasyncio.run(controller.listen(timeout=30))
This code shows how you can have your customize what each button press or Joystick movement invokes
1 2 3 4 5 6 7 8 91011121314151617181920212223
importasynciofromAsynchronousPS4ControllerimportControllerclassMyController(Controller):def__init__(self,**kwargs):Controller.__init__(self,**kwargs)asyncdefon_x_press(self):print("Hello world")asyncdefon_x_release(self):print("Goodbye world")asyncdefon_L3_up(self,value:int):print(f"Left Joystick has been pushed up by a value of {value}")asyncdefon_L3_down(self,value:int):print(f"Left Joystick has been pushed down by a value of {value}")controller=MyController(path="/dev/input/js0")# you can start listening before controller is paired, as long as you pair it within the timeout windowasyncio.run(controller.listen(timeout=30))
importasynciofromAsynchronousPS4ControllerimportControllerdefon_connect():print("I just connected")defon_disconnect():print("I just disconnected")defon_timeout():print("Timeout for waiting this controller")classMyController(Controller):def__init__(self,**kwargs):Controller.__init__(self,**kwargs)controller=MyController(interface="/dev/input/js0")asyncio.run(controller.listen(timeout=30,connect_callback=on_connect,disconnect_callback=on_disconnect,timeout_callback=on_timeout))
importasynciofromAsynchronousPS4ControllerimportControllerclassMyController(Controller):def__init__(self,**kwargs):super().__init__(self,**kwargs)asyncdefon_x_press(self):print("Hello")asyncdefon_x_release(self):print("Goodbye")classMyController2(Controller):def__init__(self,**kwargs):super().__init__(self,**kwargs)asyncdefon_x_press(self):print("2nd Controller X Press")asyncdefon_x_release(self):print("2nd Controller X Release")controller1=MyController(path="/dev/input/js0",re_listen=True)controller2=MyController2(path="/dev/input/js1",re_listen=True)loop=asyncio.get_event_loop()task_1=loop.create_task(controller1.listen())task_2=loop.create_task(controller2.listen())gathered=asyncio.gather(task_1,task_2)loop.run_until_complete(gathered)
Last update: September 28, 2023 Created: September 28, 2023