问题描述
Mac OS X 10.6 - Cocoa
我使用全局事件监视器通过自定义键盘快捷键显示状态项菜单:
globalEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent * event)
{
if([event keyCode] == kVK_F12)
{
[self handleGlobalShortcut];
//如何防止系统蜂鸣声?
}
}];
此解决方案正常工作,但系统会在用户每次按F12和 active 应用程序时产生蜂鸣声
有没有办法防止每次使用全局快捷方式时活动应用程序发出嘟嘟声?
在您的事件监视器中,您需要激活您的应用程序,以便 接收关键事件。
[NSApp activateIgnoringOtherApps:YES];
否则,事件将被传递到当前活动的应用程序>
编辑:看起来这将不起作用。
根据文档您不能修改否则阻止将事件传递到其原始目标应用程序。
所以Snow Leopard的新 addGlobalMonitorForEventsMatchingMask
API不适合处理热键。您将需要继续使用旧的Carbon RegisterEventHotKey
API。幸运的是,此API与64位Cocoa在Snow Leopard上兼容。
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?
In your event monitor, you need to activate your app so that it will receive the key event.
[NSApp activateIgnoringOtherApps:YES];
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 Carbon RegisterEventHotKey
API. Fortunately, this API is compatible with 64-bit Cocoa on Snow Leopard.
这篇关于如果在其他应用程序中按全局键盘快捷键,如何防止发出嘟嘟声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!