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

前端 未结 2 1349
心在旅途
心在旅途 2021-01-03 16:43

Mac OS X 10.6 — Cocoa

I\'m using global event monitor for displaying status item menu using custom keyboard shortcut:

globalEventMonitor         


        
相关标签:
2条回答
  • 2021-01-03 17:39

    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.

    0 讨论(0)
  • 2021-01-03 17:40

    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.

    0 讨论(0)
提交回复
热议问题