User assigned key equivalents

烂漫一生 提交于 2020-01-05 07:49:29

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/17980187/user-assigned-key-equivalents

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!