English 中文(简体)
How to prevent a Beep sound if global keyboard shortcut is pressed in the other application?
原标题:

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.

问题回答

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.





相关问题
keyboard hook in windows C++ or what?

I wish to build my own application which can send keyboard commands(messages) to the Windows OS. For example when I press the combination ctrl+shift+n, I wish to launch the notepad.exe . How can I ...

Detect if any key is pressed in C# (not A, B, but any)

[EDIT 3] I kind of "solved it" by at using the "strange" version. At least for the most important keys. It is suffient for my case, where I want to check that ALT and ALT+A are not ...

Solution for iPhone new file dialog keyboard

i want to let the user type in the name of a new file, so there are certain characters i want to prevent entry on. is there a special keyboard i can use or can i disable certain keys on the iphones ...

Search by using the keyboard in a list/grid - algorithm

I need to implement a custom search in a grid and I would like to find some user interface guidelines that explain the standard way to implement it. I mean this kind of search that is initiated by ...

热门标签