Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Unity 5] KeyCode Selection in Editor Window?

Discussion in 'Scripting' started by Studio_Akiba, Aug 10, 2020.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Is there a way to bring up some kind of selector for KeyCodes in an editor window in Unity 5?
    I have tried searching for quite a while and was unable to find anything beyond the usualy Component public KeyCode selector, which doesn't display in editor windows.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    This is what I am doing:

    Code (csharp):
    1. public KeyCode KeyToUse;
    Are you saying that does not work for you?

    This is completely counter to my experience, dating back to Unity 5.2.2f1

    I still have a few old games in Unity 5.6.6f2 and this works fine there too.

    In fact, it works fine in an array of them as well, as seen in this code in Unity5.6.6f2, line 54:

    https://github.com/kurtdekker/datas...Assets/Datasack/Input/DSUserIntentKeyboard.cs
     
  3. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    This does provide a visual selector in components (normal scripts) but not in editor windows.
    I found a workaround by using an enum popup cast as a KeyCode.

    Code (CSharp):
    1. someKeyCode = (KeyCode)EditorGUILayout.EnumPopup("Some Key:", someKeyCode);
    It's not perfect but better than nothing.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Ah, I didn't grasp this distinction in your original post. Gotcha.

    Pasting your handy workaround into my gist pile for Unity... thanks!
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Best of all, your snippet above works flawlessly, even in more recent Unity:

    Screen Shot 2020-08-10 at 3.57.00 PM.png
     
  6. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Yep, pretty sure there's some better way of doing it, maybe something undocumented in the editor, but this seems to work for my purposes.

    I just wish the editor had some built-in drawer for creating one-click KeyCodes (click a button, then the key you want on the keyboard) but the list also works, just slower.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    yeah... now THAT would be a seriously nice thing...
     
  8. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Extremely early christmas present!

    Found this on a 2-year old Unity forum, did a small mod to it to make it clearer when it is active.

    I don't think I've ever been this happy to find a piece of code. I guess that's my reward for staying up past 2am searching.

    Code (CSharp):
    1. public static KeyCode KeyCodeField(Rect controlRect, KeyCode keyCode)
    2.     {
    3.         int controlID = GUIUtility.GetControlID(FocusType.Keyboard);
    4.  
    5.         KeyCode retVal = keyCode;
    6.  
    7.         Event evt = Event.current;
    8.  
    9.         switch (evt.GetTypeForControl(controlID))
    10.         {
    11.             case EventType.Repaint:
    12.                 {
    13.                     GUIStyle style = GUI.skin.GetStyle("TextField");
    14.                     //GUIStyle style = EditorStyles.toolbar;
    15.  
    16.                     if (style == GUIStyle.none)
    17.                     {
    18.                         //Debug.Log("GUI Style not found");
    19.                     }
    20.  
    21.                     if (GUIUtility.keyboardControl == controlID)
    22.                     {
    23.                         style.Draw(controlRect, new GUIContent("Press a key"), controlID);
    24.                     }
    25.                     else
    26.                     {
    27.                         style.Draw(controlRect, new GUIContent(keyCode.ToString()), controlID);
    28.                     }
    29.                     break;
    30.                 }
    31.             case EventType.MouseDown:
    32.                 {
    33.                     if (controlRect.Contains(Event.current.mousePosition) && Event.current.button == 0 && GUIUtility.hotControl == 0)
    34.                     {
    35.                         //Debug.Log("mouse down event, control id: " + controlID + ", hot control: " + GUIUtility.hotControl);
    36.                         GUIUtility.hotControl = controlID;
    37.                         GUIUtility.keyboardControl = controlID;
    38.                         evt.Use();
    39.                     }
    40.                     break;
    41.                 }
    42.             case EventType.MouseUp:
    43.                 {
    44.                     if (GUIUtility.hotControl == controlID)
    45.                     {
    46.                         GUIUtility.hotControl = 0;
    47.                         evt.Use();
    48.                     }
    49.                     break;
    50.  
    51.                 }
    52.             case EventType.KeyDown:
    53.                 {
    54.                     //Debug.Log("key down, control id: " + controlID + ", hot control: " + GUIUtility.hotControl);
    55.                     if (GUIUtility.keyboardControl == controlID)
    56.                     {
    57.                         //Debug.Log("hotcontrol");
    58.                         retVal = Event.current.keyCode;
    59.                         GUIUtility.hotControl = 0;
    60.                         GUIUtility.keyboardControl = 0;
    61.                         evt.Use();
    62.                     }
    63.                     break;
    64.                 }
    65.             case EventType.KeyUp:
    66.                 {
    67.                     break;
    68.                 }
    69.         }
    70.         return retVal;
    71.     }
    72.  
    73.     public static KeyCode KeyCodeFieldLayout(KeyCode keyCode)
    74.     {
    75.         return KeyCodeField(EditorGUILayout.GetControlRect(), keyCode);
    76.     }
    Original can be found here
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Dude!