• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.9.5/pyobjc-42/2.5/pyobjc/pyobjc-framework-Cocoa/Examples/AppKit/HotKeyPython/
1"""HotKey.py
2
3An example that shows how to use Carbon HotKeys from a PyObjC application,
4and how to use an NSApplication subclass.
5
6To build the demo program, run this line in Terminal.app:
7
8    $ python setup.py py2app -A
9
10This creates a directory "dist" containing HotKey.app. (The
11-A option causes the files to be symlinked to the .app bundle instead
12of copied. This means you don't have to rebuild the app if you edit the
13sources or nibs.)
14"""
15
16from AppKit import *
17from PyObjCTools import AppHelper
18from Carbon.CarbonEvt import RegisterEventHotKey, GetApplicationEventTarget
19from Carbon.Events import cmdKey, controlKey
20import struct
21
22kEventHotKeyPressedSubtype = 6
23kEventHotKeyReleasedSubtype = 9
24
25class HotKeyApp(NSApplication):
26
27    def finishLaunching(self):
28        super(HotKeyApp, self).finishLaunching()
29        # register cmd-control-J
30        self.hotKeyRef = RegisterEventHotKey(38, cmdKey | controlKey, (0, 0),
31                                             GetApplicationEventTarget(), 0)
32
33    def sendEvent_(self, theEvent):
34        if theEvent.type() == NSSystemDefined and \
35               theEvent.subtype() == kEventHotKeyPressedSubtype:
36            self.activateIgnoringOtherApps_(True)
37            NSRunAlertPanel(u'Hot Key Pressed', u'Hot Key Pressed',
38                None, None, None)
39        super(HotKeyApp, self).sendEvent_(theEvent)
40
41if __name__ == "__main__":
42    AppHelper.runEventLoop()
43