如果在其他应用程序中按下全局键盘快捷键,如何防止发出哔声? [英] How to prevent a Beep sound if global keyboard shortcut is pressed in the other application?

查看:16
本文介绍了如果在其他应用程序中按下全局键盘快捷键,如何防止发出哔声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?
    }
}];

此解决方案有效,但每次用户按 F12 并且 active 应用程序没有响应此按键事件时,系统都会发出哔声.

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).

看起来这行不通.

根据文档您不能修改或以其他方式阻止将事件传递到其原始目标应用程序".

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

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.

这篇关于如果在其他应用程序中按下全局键盘快捷键,如何防止发出哔声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆