8

Cocoa has a plethora of integer masks and codes. For instance, NSCommandKeyMask or NSF1FunctionKey, which are clearly defined and documented in the headers.

However, some can be archaic and when accessing accessibility attributes, for instance to get the glyph (AXMenuItemCmdGlyph), you're given an integer number like 111, which represents F1 (0xf704), or 112 which represents F2 (hex: 0xf705).

Is there an easy way to deal with masks and codes? Perhaps one that's able to convert the 111 into the corresponding hex unicode 0xf704? What I mean is that NSEvent.h maps NSF1FunctionKey to 0xf704, but is there a mapping for 111 to NSF1FunctionKey or 0xf704?

the979kid
  • 635
  • 5
  • 15
  • I don't understand, why don't you use them as constants? – Maz Jul 24 '10 at 14:41
  • I would if I could find the constants in any of the header files. Are there constants for F1-F13 key for instance? I could map my own constants, but wouldn't want to reinvent the wheel if it exists. – the979kid Jul 24 '10 at 14:43
  • If you tell me which header files/classes you're using, I'll take a look. My guess is that there is a base constant, like (I'm just making this up) FUNCTION_KEY_ONE and then to get F2 it is FUNCTION_KEY_ONE+1 Just in looking back up at your question, this appears to be the case. – Maz Jul 24 '10 at 15:39
  • Hisham: The Accessibility attribute constants are defined in AXAttributeConstants.h. – Peter Hosey Jul 24 '10 at 16:21

1 Answers1

1

If you want the Unicode character for the menu item's key equivalent, rather than the glyph for it, try getting the kAXMenuItemCmdCharAttribute attribute instead of kAXMenuItemCmdGlyphAttribute.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • I'm looking for the glyphs, kAXMenuItemCmdCharAttribute will give null for anything other than characters. Anyway, the best thing I found was to basically map the glyph codes (111, etc) given by the AXMenuItemCmdGlyph attribute to their unicode/NSEvent.h equivalents. There does not seems to be any relationship between the accessibility codes given to glyphs and the actual unicodes for these glyphs as given in NSEvent.h. If someone knows better, please share. – the979kid Jul 27 '10 at 20:30
  • If you want to compare to the character constants, then you're looking for characters. What's an example of a menu item that has a `CmdGlyph` but not a `CmdChar`? – Peter Hosey Jul 27 '10 at 22:01
  • Example: "Force Quit…" has glyph 27 and the char is null. However it does have a virtual key code 53 which corresponds to `kVK_Escape`. – Nick Moore Dec 13 '11 at 10:11
  • @NickMoore: Interesting. Well, you can't convert a glyph into a character, since many characters could have the same glyph, but you can (and generally should) respond to the key code, and you can render a glyph as described in this recent other question: http://stackoverflow.com/q/8472791/30461 – Peter Hosey Dec 13 '11 at 14:27
  • My own code assumes menu items with shortcuts will have either a key code or character (or possibly both, but I've never seen it; key codes only are used for special keys like escape and the F keys). I ignore the glyph. It's a red herring, unless you are specifically rendering it to the screen. – Nick Moore Dec 14 '11 at 13:39