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") = wx.Joystick()
        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()

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:

  1. Open Control Panel --> Game Controllers

  2. Click Advanced button

  3. In the dialog box that pops up make sure that there is a Preferred device selected (i.e. it's NOT set to none)

  4. Click OK and you should now be receiving events using the sample code above.

JoystickEvents (last edited 2008-03-11 10:50:39 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.