Created
June 29, 2012 22:06
-
-
Save cuilkid/3020934 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Cocoa import NSApp, NSEvent, NSKeyDownMask # , NSApplication | |
from Foundation import NSObject, NSLog | |
from PyObjCTools import AppHelper | |
from AppKit import NSApplication | |
# https://bitbucket.org/ronaldoussoren/pyobjc/ | |
# you have to enable access for assistive devices in 'System Preferences>Universal Access>Keyboard' | |
class AppDelegate(NSObject): | |
def applicationDidFinishLaunching_(self, notification): | |
print 'A' | |
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSKeyDownMask, handler) | |
print 'B' | |
def handler(event): | |
try: | |
print 'C' | |
NSLog(u"%@", event) | |
print 'D' | |
except KeyboardInterrupt: | |
AppHelper.stopEventLoop() | |
def main(): | |
app = NSApplication.sharedApplication() | |
delegate = AppDelegate.alloc().init() | |
NSApp().setDelegate_(delegate) | |
print 'E' | |
AppHelper.runEventLoop() | |
if __name__ == '__main__': | |
main() |
I installed Python2.7 and now none of the imports work...
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
from Foundation import NSObject, NSLog
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named Foundation
from PyObjCTools import AppHelper
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named PyObjCTools
You need to install the dependencies again for python2.7
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your code works for me now. I'm on Python2.7 and using the pyobjc in the link you have above.
I am however not able to send KeyboardInterrupt to abort the process.