如果在其他应用程序中按下全局键盘快捷键,如何防止发出蜂鸣声?
Mac OS X 10.6 - Cocoa
我正在使用全局事件监视器来使用自定义键盘快捷键显示状态项菜单: <代码>
globalEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event)
{
if ([event keyCode] == kVK_F12)
{
[self handleGlobalShortcut];
// How to prevent system beep?
}
}];
This solution is working but the system generates a beep sound every time when user presses F12 and active application doesn't respond to this key event.有什么方法可以防止活动应用程序在每次使用全局快捷方式时发出蜂鸣声?
Mac OS X 10.6 — Cocoa
I'm using global event monitor for displaying status item menu using custom keyboard shortcut:
globalEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event) { if ([event keyCode] == kVK_F12) { [self handleGlobalShortcut]; // How to prevent system beep? } }];
This solution is working but the system generates a beep sound every time when user presses F12 and active application doesn't respond to this key event.
Is there any way to prevent an active application from beeping every time I use a global shortcut?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在事件监视器中,您需要激活您的应用程序,以便它接收按键事件。
否则,事件将被传递到当前活动的应用程序(将发出蜂鸣声)。
编辑:看起来这行不通。
根据文档“您无法修改或以其他方式阻止事件传递到其原始目标应用程序”。
因此,Snow Leopard 的新
addGlobalMonitorForEventsMatchingMask
API 不适合处理热键。您将需要继续使用旧的 CarbonRegisterEventHotKey
API。幸运的是,这个 API 与 Snow Leopard 上的 64 位 Cocoa 兼容。In your event monitor, you need to activate your app so that it will receive the key event.
Otherwise, the event will be passed to the current active application (which will beep).
EDIT: It looks like this won't work.
According to the docs "you cannot modify or otherwise prevent the event from being delivered to its original target application".
So Snow Leopard's new
addGlobalMonitorForEventsMatchingMask
API is not suitable for handling hot keys. You will need to continue to use the old CarbonRegisterEventHotKey
API. Fortunately, this API is compatible with 64-bit Cocoa on Snow Leopard.看来不可能。蜂鸣声是 [NSResponder noResponderFor] 的默认行为。因此,应用程序会发出蜂鸣声,除非它通过添加最后一个响应者来覆盖该行为,而这在应用程序外部不太可能实现。
Seems impossible. Beeping is the default behavior of [NSResponder noResponderFor]. So an application beep unless it override that behavior by adding a last responder, which is unlikely doable from outside the application.