Introduction
There are two ways of reading joystick data in wx. Event driven and polling, both of which are described in the wxPython demos. Here I will only describe the event driven way since the official demo focuses on the polling method. In general, joystick events are no different than your typical wxWindow events. They are bound and handled the same way. However, there is a potential caveat on MS Windows as described below.
Code Sample
Here is an example of catching joystick button clicks (works on Windows, Linux and Mac):
import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, "Joystick test") self.joy = wx.Joystick() self.joy.SetCapture(self) self.Bind(wx.EVT_JOY_BUTTON_DOWN, self.onJoyBtn) self.Bind(wx.EVT_JOY_BUTTON_UP, self.onJoyBtn) def onJoyBtn(self, event): print "Joystick button pressed" app = wx.App(0) frame = MyFrame() app.SetTopWindow(frame) app.MainLoop()
MS Windows Caveat
As mentioned earlier, there is a potential that this code will not generate any events on MS Windows (I tried XP only). Your joystick will be recognized by Windows and it will show up and react correctly in the "Game Controllers" control panel but you still might not get any events in your wx app. Well, the solution is as follows:
Open Control Panel --> Game Controllers
Click Advanced button
In the dialog box that pops up make sure that there is a Preferred device selected (i.e. it's NOT set to none)
- Click OK and you should now be receiving events using the sample code above.