How to prevent a Beep sound if global keyboard sho

2019-02-11 09:01发布

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?

2条回答
趁早两清
2楼-- · 2019-02-11 09:33

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.

查看更多
爷、活的狠高调
3楼-- · 2019-02-11 09:48

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.

查看更多
登录 后发表回答