Pergunta

I'm working on a Status Bar App. I'd like to allow the user to modify the menu item key equivalents to their own preferences. I've seen this done before it's a pretty common feature. A prefs window usually has an area with textfields where the user enters their keyboard shortcut for specific menu items.

How does one setup the textfield so that it displays the modifier key fonts? The default NSTextfield ignores modifiers.

Also I have yet to find an example project showing this functionality, if anyone has a link that would be very helpful.

Foi útil?

Solução

You may wish to take a look at Shortcut Recorder which allows the user to record key equivalents using modifiers and then for you to retrieve them and set them for the NSMenuItem.

Once the user has recorder their shortcut/key combination you can access the SRRecorderControl's objectValue property which has values for the key code and modifier flags.

https://github.com/Kentzo/ShortcutRecorder

Outras dicas

The modifier keys all have Unicode values that you can see in the "Special Characters" palette and they are displayed by the normal system font. Most of them are in the "Technical Symbols" category of the palette but the up-arrows for the Shift and Caps Lock keys are in the "Arrows" section.

You can insert these values when editing label elements in Interface Builder or Xcode, or include their corresponding Unicode values when creating something like an NSString object.

For example, the Shift key's UTF-8 sequence (as shown in the palette) is E2 87 A7 so one way to set it programmatically is to add those bytes to an array and create an NSString from the UTF-8 array. I prefer the array approach because it's "bulletproof"; it will always be interpreted as UTF-8 and do what you expect. If you try to insert characters directly into @"" or CFSTR() strings, you then have to make sure your source file's encoding is correct (and older versions of Xcode wouldn't even allow this, they'd assume ASCII only).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top