- Home /
Escaping the # & % _ hotkey modifiers in a menuItem
How do you use the shortcut modifiers (#, &, %, _) in a MenuItem string without them being interpreted at hotkey modifiers?
For example:
[MenuItem("File/Foo & Bar %l")]
should show up as File > Foo & Bar ⌘L, and not File > Foo ⌥Space.
Clearly it is possible, as File > Build & Run ⌘B does it.
Answer by roojerry · Aug 25, 2014 at 01:38 PM
I may have stumbled on a way to solve this issue. It's not really pretty, but it seems to work. Since the hotkeys are picked up after a whitespace character, you need to place a character before the "&" that won't/can't be displayed by Unity. I did this by escaping an undefined ASCII symbol(in hex) right before the "&". (Any in the range of 7F to 9F)
for example:
[MenuItem("File/This \x8B& That")]
Sneaky. Unless a less hacky answer comes along, I'll accept this as the answer.
I played around with this a little more. Ins$$anonymous$$d of using an "undefined" ASCII character, there is a "non-breaking space" (hex A0) that is displayed as a space, but doesn't get interpreted as the start of a hotkey definition.
[$$anonymous$$enuItem("File/This\xA0& That")]
A little less hacky, but I am also still curious how Unity does this internally.
I'm quite struggling with this, could you provide an example with the shortcut letter 'a' and a [$$anonymous$$enuItem("Window/Test")]?
Answer by zwcloud · Aug 13, 2018 at 01:52 PM
The "Best Answer" doesn't work for me on Unity 5.6.6f2.
I'm using the Fullwidth Ampersand \uff06
, i.e. &
.
[MenuItem("Window/Mesh Terrain Editor/Help/Feature Request \uff06 Bug Report", false, 9004)]
Your answer
Follow this Question
Related Questions
MenuItem toggle getting reset on play. 0 Answers
How do I add a MenuItem to Hierarchy's Create Menu? 2 Answers
Editor Scripting Question 1 Answer
Can I create MenuItem from code? 3 Answers