在 Swift 项目上快速使用 MASShortcut 框架
Quick use of MASShortcut framework on Swift project
我在 Swift OSX 项目中实现 MASShortcut (docs here) 以侦听全局热键时遇到问题。我已经设法通过 CocoaPods 导入了框架,并且我有一个工作的 MASShortcutView 实例:
@IBOutlet weak var testShortcutView:MASShortcutView!
我还想出了如何使用快捷方式监控和触发某些东西(请告诉我这是否正确):
let shortcut = MASShortcut(keyCode: keycode, modifierFlags: modifierkeys)
MASShortcutMonitor.sharedMonitor().registerShortcut(shortcut, withAction: callback)
The question here is, how can I get the keyCode and modifierFlags from my MASShortcutView?
非常感谢您,我到处搜索,但在 swift 上找不到有关如何执行此操作的示例。我能找到的只有objective-c,我怎么也想不通
以下代码将为 Cmd+Shift+K 组合键注册快捷方式处理程序
let shortcut = MASShortcut.init(keyCode: UInt(kVK_ANSI_K), modifierFlags: UInt(NSEventModifierFlags.CommandKeyMask.rawValue + NSEventModifierFlags.ShiftKeyMask.rawValue))
MASShortcutMonitor.sharedMonitor().registerShortcut(shortcut, withAction: {
print("Hello world")
})
Cmd 和 Shift - 修改键。您应该在 modifierFlags 参数中设置它们。 NSEventModifierFlags 枚举中提供了可能值的完整列表。
为方便起见,我已将示例项目放在 github:
https://github.com/melifaro-/ShortCutSwiftSample
处理快捷方式更改的:
shortcutView.shortcutValueChange = { (sender) in
let callback: (() -> Void)!
if self.shortcutView.shortcutValue.keyCodeStringForKeyEquivalent == "k" {
callback = {
print("K shortcut handler")
}
} else {
callback = {
print("Default handler")
}
}
MASShortcutMonitor.sharedMonitor().registerShortcut(self.shortcutView.shortcutValue, withAction: callback)
}
我已将更改推送到存储库中。我建议尝试以下场景:
使用快捷视图:
设置 Cmd+Shift+K 快捷方式
设置 Cmd+Shift+J 快捷方式
试试这个快捷方式 - 应该执行不同的回调
希望对您有所帮助。
在Swift5
let shortcut = MASShortcut(keyCode: kVK_ANSI_K, modifierFlags: [.command, .shift])
MASShortcutMonitor.shared()?.register(shortcut, withAction: {
print("hello")
})
我在 Swift OSX 项目中实现 MASShortcut (docs here) 以侦听全局热键时遇到问题。我已经设法通过 CocoaPods 导入了框架,并且我有一个工作的 MASShortcutView 实例:
@IBOutlet weak var testShortcutView:MASShortcutView!
我还想出了如何使用快捷方式监控和触发某些东西(请告诉我这是否正确):
let shortcut = MASShortcut(keyCode: keycode, modifierFlags: modifierkeys)
MASShortcutMonitor.sharedMonitor().registerShortcut(shortcut, withAction: callback)
The question here is, how can I get the keyCode and modifierFlags from my MASShortcutView?
非常感谢您,我到处搜索,但在 swift 上找不到有关如何执行此操作的示例。我能找到的只有objective-c,我怎么也想不通
以下代码将为 Cmd+Shift+K 组合键注册快捷方式处理程序
let shortcut = MASShortcut.init(keyCode: UInt(kVK_ANSI_K), modifierFlags: UInt(NSEventModifierFlags.CommandKeyMask.rawValue + NSEventModifierFlags.ShiftKeyMask.rawValue))
MASShortcutMonitor.sharedMonitor().registerShortcut(shortcut, withAction: {
print("Hello world")
})
Cmd 和 Shift - 修改键。您应该在 modifierFlags 参数中设置它们。 NSEventModifierFlags 枚举中提供了可能值的完整列表。
为方便起见,我已将示例项目放在 github:
https://github.com/melifaro-/ShortCutSwiftSample
处理快捷方式更改的:
shortcutView.shortcutValueChange = { (sender) in
let callback: (() -> Void)!
if self.shortcutView.shortcutValue.keyCodeStringForKeyEquivalent == "k" {
callback = {
print("K shortcut handler")
}
} else {
callback = {
print("Default handler")
}
}
MASShortcutMonitor.sharedMonitor().registerShortcut(self.shortcutView.shortcutValue, withAction: callback)
}
我已将更改推送到存储库中。我建议尝试以下场景:
使用快捷视图:
设置 Cmd+Shift+K 快捷方式
设置 Cmd+Shift+J 快捷方式
试试这个快捷方式 - 应该执行不同的回调
希望对您有所帮助。
在Swift5
let shortcut = MASShortcut(keyCode: kVK_ANSI_K, modifierFlags: [.command, .shift])
MASShortcutMonitor.shared()?.register(shortcut, withAction: {
print("hello")
})