在OS X(狮子)如何找到一个组合键是否被用作键盘快捷键? [英] In OS X (lion) how can I find whether a key combination is in use as a keyboard shortcut?

查看:220
本文介绍了在OS X(狮子)如何找到一个组合键是否被用作键盘快捷键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序(OS X狮子)中放置一堆键盘快捷键,所以我可以从键盘做大多数事情。当然有一堆热键组合的列表已经使用,包括在HIG中的一个。

I want to put a bunch of keyboard shortcuts in my app (OS X lion) so I can do most things from the keyboard. There are of course a bunch of lists of hot key combos in use already, including the one in the HIG.

有一些实用程序可以用来键入一个键组合,并找出它是否已经意味着什么(全局或mac标准 - 我不太担心重用一些特殊组合使用的另一个应用程序 - 或者我应该?)

Is there some utility that can be used to type a key combination and find out if it already means something (either globally, or mac standard -- I'm not too worried about reusing some special combo used by another app -- or should I be?)?

推荐答案

您可以使用Carbon来做到这一点。不要害怕在这里使用Carbon,没有Cocoa的方式来获取这些信息,并且Carbon方法仍然支持。

You can use Carbon to do this. Don't be afraid to use Carbon here, there is no Cocoa way to get this information and the Carbon methods are still supported.

CopySymbolicHotKeys ()函数返回一个字典数组,其中包含有关在键盘首选项窗格中定义的系统范围的符号热键的信息。每个字典包含有关单个热键的信息。

The CopySymbolicHotKeys() function returns an array of dictionaries, containing information about the system-wide symbolic hot keys defined in the Keyboard preferences pane. Each dictionary contains information about a single hot key.

具体来说,每个字典有三个键:

Specifically, each dictionary has three keys:


  • kHISymbolicHotKeyCode :热键的虚拟键代码,表示为 CFNumber

  • kHISymbolicHotKeyModifiers :热键的键盘修饰符,表示为 CFNumber

  • kHISymbolicHotKeyEnabled :热键的启用状态,表示为 CFBoolean

  • kHISymbolicHotKeyCode: The virtual key code of the hot key, represented as a CFNumber.
  • kHISymbolicHotKeyModifiers: The hot key’s keyboard modifiers, represented as a CFNumber.
  • kHISymbolicHotKeyEnabled: The enabled state of the hot key, represented as a CFBoolean.

显然,这些都是原始键代码,所以如果你想看看键代码实际上是什么to。

Obviously these are raw key codes so you will need to do some work if you want to see what the key codes actually refer to.

请注意,该数组不包含自定义的应用程序特定的热键,但这是一个小问题。

Note that the array doesn't contain custom, application-specific hotkeys, but this is a minor problem.

这里有一个简单的例子:

Here's a simple example:

#import <Carbon/Carbon.h>
CFArrayRef registeredHotKeys;


if(CopySymbolicHotKeys(&registeredHotKeys) == noErr)
{
    CFIndex count = CFArrayGetCount(registeredHotKeys);
    for(CFIndex i = 0; i < count; i++)
    {
        CFDictionaryRef hotKeyInfo = CFArrayGetValueAtIndex(registeredHotKeys, i);

        CFNumberRef hotKeyCode = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyCode);
        CFNumberRef hotKeyModifiers = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyModifiers);
        CFBooleanRef hotKeyEnabled = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyEnabled);

        NSLog(@"key code: %@ modifiers: %@ enabled: %@", hotKeyCode, hotKeyModifiers, hotKeyEnabled);

    }

    //you MUST release the dictionary when finished with it
    CFRelease(registeredHotKeys);
}

请记住,您需要将Carbon框架添加到链接二进制库和库构建阶段。

Remember that you'll need to add the Carbon framework to the Link Binary with Libraries build phase in your project settings.

有关详细信息,请参阅 Carbon Event Manager文档(11Mb PDF)。

For more information you should look at the Carbon Event Manager docs (11Mb PDF).

这篇关于在OS X(狮子)如何找到一个组合键是否被用作键盘快捷键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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