How to use Mac OS X NSEvents within wxPython application?
<p>I am writing an application that has to react to system wide keypresses on Mac OS X.</p>

<p>So I found some key logger examples that should work and hit a wall, because all examples are based on NSSharedApplication() and PyObjC AppHelper.runEventLoop() while my application is written in wxPython.</p>

<p>Here I post a modification of the simplest example from <a href="https://github.com/ljos" rel="nofollow">https://github.com/ljos</a>
that I thought it should work. But it does not.</p>

<pre><code>from AppKit import *
import wx

class AppDelegate(NSObject):
    def applicationDidFinishLaunching_(self, aNotification):
        NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSKeyDownMask, handler)

def handler(event):
    print (u"%@", event)

app = wx.App()
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
app.MainLoop()
</code></pre>

<p>It is obvious that the MainLoop() doesn't catch the delegated NSEvents.</p>

<p>After app = wx.App() the NSApp() is returned correctly. So why doesn't this work? How do I make it work?</p>

<p>As nobody answered I went searching around with different angle in view.</p>

<p>So I discovered that Quartz module can be used to get to keyboard and mouse events. No custom loop needed, therefore wx.App() and wx.App.MainLoop() aren't getting in the way.</p>

<p>I also found a nice package named pynput that does just that for me, thus sparing me plenty of time. Quartz is pretty complicated, a lot of scrambled names for functions and constants. But it does a good job.</p>

python osx cocoa wxpython pyobjc