如何在 Mac OS X 上的 Python .app 中实现全局热键?
我目前正在修改一个由我组织内的某人编写的应用程序,但该人无法再维护它。我正在尝试实现一个系统范围的全局热键,按下该热键即可将应用程序窗口置于焦点中。
我在网上遇到的唯一实现是通过他们的 HotKeyPython 示例中的 PyObjC 实现。该示例虽然使用了 Carbon,但在我测试它时似乎根本不起作用。
这是我正在尝试的方法,但似乎不起作用:
from Foundation import *
from AppKit import *
from Carbon.CarbonEvt import RegisterEventHotKey
from Carbon.Events import cmdKey, controlKey, optionKey, shiftKey
import objc, AppLauncher
import platform
import struct
IS_SNOW_LEOPARD = platform.mac_ver()[0].startswith('10.6')
if IS_SNOW_LEOPARD:
from Quartz import *
kEventHotKeyPressedSubtype = 6
kEventHotKeyReleasedSubtype = 9
class AppLauncher(NSObject):
window = objc.IBOutlet()
view = objc.IBOutlet()
field = objc.IBOutlet()
def awakeFromNib(self):
self.window.makeFirstResponder_(self.field)
self.field.selectText_(self)
self.field.setDelegate_(self)
self.controlTextDidChange_(None)
def applicationDidFinishLaunching(self):
# register cmd-control-J
self.hotKeyRef = RegisterEventHotKey(38, cmdKey + controlKey, (0, 0),
sendEvent_(), 0)
def sendEvent_(self, theEvent):
if theEvent.type() == NSSystemDefined and \
theEvent.subtype() == kEventHotKeyPressedSubtype:
self.activateIgnoringOtherApps_(True)
NSRunAlertPanel(u'Hot Key Pressed', u'Hot Key Pressed',
None, None, None)
super(AppLauncher, self).sendEvent_(theEvent)
关于如何让这个工作起作用(最好不涉及碳)有什么想法吗?
多谢。
I'm currently modifying an app that was written by someone within my organization that no longer is able to maintain it. I am attempting to implement a system-wide global hot key that will simply bring the app window into focus when pressed.
The only implementation of this I came across online was through PyObjC in their HotKeyPython example. That example uses Carbon though, and did not appear to function at all when I was testing it.
Here's what I was trying which did not appear to work:
from Foundation import *
from AppKit import *
from Carbon.CarbonEvt import RegisterEventHotKey
from Carbon.Events import cmdKey, controlKey, optionKey, shiftKey
import objc, AppLauncher
import platform
import struct
IS_SNOW_LEOPARD = platform.mac_ver()[0].startswith('10.6')
if IS_SNOW_LEOPARD:
from Quartz import *
kEventHotKeyPressedSubtype = 6
kEventHotKeyReleasedSubtype = 9
class AppLauncher(NSObject):
window = objc.IBOutlet()
view = objc.IBOutlet()
field = objc.IBOutlet()
def awakeFromNib(self):
self.window.makeFirstResponder_(self.field)
self.field.selectText_(self)
self.field.setDelegate_(self)
self.controlTextDidChange_(None)
def applicationDidFinishLaunching(self):
# register cmd-control-J
self.hotKeyRef = RegisterEventHotKey(38, cmdKey + controlKey, (0, 0),
sendEvent_(), 0)
def sendEvent_(self, theEvent):
if theEvent.type() == NSSystemDefined and \
theEvent.subtype() == kEventHotKeyPressedSubtype:
self.activateIgnoringOtherApps_(True)
NSRunAlertPanel(u'Hot Key Pressed', u'Hot Key Pressed',
None, None, None)
super(AppLauncher, self).sendEvent_(theEvent)
Any thoughts on how I can get this working (preferably without getting Carbon involved)?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论