Mac托盘应用程序的全局键盘快捷键

时间:2021-09-30 05:55:51

I am developing a Mac app using Swift 4 which only runs as an Agent (it only has a menu in the tray). I'm completely new to Swift.

我正在使用Swift 4开发一个Mac应用程序,它只作为代理程序运行(它只在托盘中有一个菜单)。我对Swift完全不熟悉。

I want it to respond to keyboard shortcuts, but the basic shortcuts (see picture) only work when the app has focus (in my case, when the menu is being clicked).

我希望它能够响应键盘快捷键,但基本快捷键(参见图片)仅在应用程序具有焦点时才会起作用(在我的情况下,单击菜单时)。

Mac托盘应用程序的全局键盘快捷键

So what I need is for my app to respond to system-wide keyboard shortcuts.

所以我需要的是我的应用程序响应系统范围的键盘快捷键。

I've tried:

  • HotKey but it doesn't seem to be compatible with Swift 4, or perhaps I can't use Swift Package Manager
  • HotKey但它似乎与Swift 4不兼容,或者我可能无法使用Swift Package Manager

  • this answer but again Swift 4 raises several errors that I couldn't solve
  • 这个答案,但Swift 4再次引发了一些我无法解决的错误

Has anyone done it?

有人做过吗?

1 个解决方案

#1


3  

So I ended up getting it to work with HotKey:

所以我最终得到它与HotKey一起工作:

  • by using Carthage as a package manager instead of SPM
  • 使用Carthage作为包管理器而不是SPM

  • and by not using HotKey's example code from their README, which for some reason didn't work for me, but the one in their example app.
  • 并且不使用自己的自述文件中的HotKey示例代码,由于某些原因这对我来说不起作用,而是在他们的示例应用程序中使用。

It works great!

它很棒!

#1


3  

So I ended up getting it to work with HotKey:

所以我最终得到它与HotKey一起工作:

  • by using Carthage as a package manager instead of SPM
  • 使用Carthage作为包管理器而不是SPM

  • and by not using HotKey's example code from their README, which for some reason didn't work for me, but the one in their example app.
  • 并且不使用自己的自述文件中的HotKey示例代码,由于某些原因这对我来说不起作用,而是在他们的示例应用程序中使用。

It works great!

它很棒!