How to prevent a Beep sound if global keyboard shortcut is pressed in the other application?

只愿长相守 提交于 2019-11-30 16:21:14

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1880555/how-to-prevent-a-beep-sound-if-global-keyboard-shortcut-is-pressed-in-the-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!