如果在其他应用程序中按下全局键盘快捷键,如何防止发出蜂鸣声?

发布于 2024-08-15 04:15:02 字数 579 浏览 5 评论 0原文

Mac OS X 10.6 - Cocoa

我正在使用全局事件监视器来使用自定义键盘快捷键显示状态项菜单: <代码>

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.

有什么方法可以防止活动应用程序在每次使用全局快捷方式时发出蜂鸣声?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把昨日还给我 2024-08-22 04:15:02

在事件监视器中,您需要激活您的应用程序,以便接收按键事件。

[NSApp activateIgnoringOtherApps:YES];

否则,事件将被传递到当前活动的应用程序(将发出蜂鸣声)。

编辑:看起来这行不通。

根据文档“您无法修改或以其他方式阻止事件传递到其原始目标应用程序”。

因此,Snow Leopard 的新 addGlobalMonitorForEventsMatchingMask API 不适合处理热键。您将需要继续使用旧的 Carbon RegisterEventHotKey API。幸运的是,这个 API 与 Snow Leopard 上的 64 位 Cocoa 兼容。

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.

单调的奢华 2024-08-22 04:15:02

看来不可能。蜂鸣声是 [NSResponder noResponderFor] 的默认行为。因此,应用程序会发出蜂鸣声,除非它通过添加最后一个响应者来覆盖该行为,而这在应用程序外部不太可能实现。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文