How to get the "Nice" name of a KeyCode

So I have a KeyCode and I need the “nice” (user-facing) name, such as KeyCode.Alpha1 = "1", KeyCode.Escape = "Esc", and KeyCode.Comma = ",". Thanks!

Well, it depends on what you consider nice ^^. Just converting it to a string does use the enum names:

KeyCode key = KeyCode.Escape;
string name = key.ToString(); // --> "Escape"

If you don’t like the enum names you have to setup your own conversion. You can simply create an array of strings where the index represents the keycode. That’s of course a quite long array. If you don’t want to manually initialize all values, you can initialize it with the enum values and just overwrite those which you want to appear different. Since some array indices wouldn’t be in use (since KeyCode has some holes) you could also use a Dictionary instead.

Dictionary keyNames = new Dictionary();

foreach (KeyCode k in Enum.GetValues(typeof(KeyCode)))
    keyNames.Add(k, k.ToString());
// replace Alpha0, Alpha1, .. and Keypad0... with "0", "1", ...
for (int i = 0; i < 10; i++){
    keyNames[(KeyCode)((int)KeyCode.Alpha0+i)] = i.ToString();
    keyNames[(KeyCode)((int)KeyCode.Keypad0+i)] = i.ToString();
}
keyNames[KeyCode.Comma] = ",";
keyNames[KeyCode.Escape] = "Esc";
// ...

Once initialized you can simply get your custom “name” like this:

KeyCode key = KeyCode.Escape;
string name = keyNames[key]; // --> "Esc"