如何在 Mac OS X 上的 Python .app 中实现全局热键?

发布于 2024-10-27 05:59:43 字数 1520 浏览 7 评论 0原文

我目前正在修改一个由我组织内的某人编写的应用程序,但该人无法再维护它。我正在尝试实现一个系统范围的全局热键,按下该热键即可将应用程序窗口置于焦点中。

我在网上遇到的唯一实现是通过他们的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文