-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptIn.py
35 lines (27 loc) · 927 Bytes
/
OptIn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# cocoa_keypress_monitor.py by Bjarte Johansen is licensed under a
# License: http://ljos.mit-license.org/
# https://gist.github.com/ljos/3019549
from AppKit import NSApplication, NSApp
from Foundation import NSObject, NSLog
from Cocoa import NSEvent, NSKeyDownMask
from PyObjCTools import AppHelper
import time
import sys
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, notification):
mask = NSKeyDownMask
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, handler)
def handler(event):
sys.stdout.write(event.characters().description())
sys.stdout.flush()
def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
try:
AppHelper.runEventLoop()
except KeyboardInterrupt as e:
AppHelper.stopEventLoop()
sys.exit(0)
if __name__ == '__main__':
main()