unexpected error

Eric Le Lay <[email protected]> Wed, 8 Oct 2014 12:48:00 +0200
Newsgroups gmane.comp.python.pyobjc.devel
Message-ID <[email protected]>
Hello list,

I've encountered a problem when trying to register and event tap:
The error occurs when calling loop.addPort_forMode_(port, NSDefaultRunLoopMode)
in test_app.py, included below.
Can someone see what I'm doing wrong?

My code is an adaptation of the first response in
http://forums.macrumors.com/showthread.php?t=1145465
The objective-c version does work.

Thanks,
Eric
________________ test_app.py ___________________________
#Traceback (most recent call last):
#   File "/Users/elelay/Desktop/test_app.py", line 35, in <module>
#     eventTap.runEventsCapture()
#   File "/Users/elelay/Desktop/test_app.py", line 30, in runEventsCapture
#     loop.addPort_forMode_(port, NSDefaultRunLoopMode)
# ValueError: NSInvalidArgumentException - *** -scheduleInRunLoop:forMode: cannot be sent to an abstract object of class NSMachPort: Create a concrete instance!
#

from AppKit import NSSystemDefined, NSApp
import Quartz
from CoreFoundation import CFMachPortGetPort
from Foundation import NSMachPortDeallocateNone, \
                       NSRunLoop, NSDefaultRunLoopMode, \
                       NSMachPort

class MacKeyEventsTap(object):
    def __init__(self):
        pass

    def eventTap(self, proxy, type_, event, refcon):
        print("eventTap()")
        return event

    def runEventsCapture(self):
        tap = Quartz.CGEventTapCreate(
            Quartz.kCGSessionEventTap,
            Quartz.kCGHeadInsertEventTap,
            Quartz.kCGEventTapOptionDefault,
            # NSSystemDefined for media keys
            Quartz.CGEventMaskBit(NSSystemDefined),
            self.eventTap,
            None
        )
        port = NSMachPort.alloc().initWithMachPort_options_(CFMachPortGetPort(tap), NSMachPortDeallocateNone)
        loop = NSRunLoop.currentRunLoop()
        loop.addPort_forMode_(port, NSDefaultRunLoopMode)
        NSApp.run()

if __name__ == '__main__':
    eventTap = MacKeyEventsTap()
    eventTap.runEventsCapture()

------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk