我正在开发一个使用伟大的 MASShortcut repository 的应用程序什帕科夫斯基。我引用这个是为了询问是否有一种方法可以像 Shpakovski 在他的存储库中那样注册 Capslock 的快捷方式?
我似乎无法将 0xffff(Key Codes 显示为 Capslock)注册为有效的快捷方式。即使我将它与修饰键结合起来。我还想在按下按钮切换系统的大写锁定功能之前捕获它。
我知道我可以监听所有击键并在发生大写锁定时捕获它,但我想避免这种行为,因为我觉得它可能不安全并且还会降低整体系统性能。
编辑:我已经看过一些适用于 Windows 的 C++/C# 库,它们实现了此功能。他们似乎不断地覆盖系统的大写锁定标志,我觉得这很奇怪。我基本上有两个问题:
<强>1。如何在没有击键监听器的情况下捕获 Capslock 按下
<强>2。如何在不触发系统大写锁定功能的情况下执行此操作。
最佳答案
一种可能是在您的 NSApplication
delegate
中覆盖 sendEvent:
。来自 Apples documentation :
You rarely invoke sendEvent: directly, although you might want to override this method to perform some action on every event. sendEvent: messages are sent from the main event loop (the run method). sendEvent: is the method that dispatches events to the appropriate responders—NSApp handles application events, the NSWindow object indicated in the event record handles window-related events, and mouse and key events are forwarded to the appropriate NSWindow object for further dispatching.
您可以评估当前事件是否按下了修饰键并据此采取行动。
编辑:
用于后台处理Quartz event taps可以使用。我不知道这是否会导致拒绝。
关于macos - 在 Cocoa 应用程序中捕获 Capslock 按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21849287/