Search Unity

Listen for key up/down in editor mode?

Discussion in 'Scripting' started by homer_3, Sep 28, 2020.

  1. homer_3

    homer_3

    Joined:
    Jun 19, 2011
    Posts:
    111
    Is there any way to listen for a key press in editor mode? I found some posts that said you could use OnSceneGUI to listen for events, but those don't work. You don't get key events, just mouse move and repaint. They also require extending Editor which is not ideal.
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Event is probably what you're looking for.
    To read a key-press:
    Code (CSharp):
    1. //Returns true if any key was pressed.
    2. if(Event.current.isKey)
    3. {
    4.    //The key that was pressed.
    5.    KeyCode key = Event.current.KeyCode;
    6. }
    Additionally, if you need to detect if the Shift or Ctrl keys were pressed, there is a shortcut for those:
    Code (CSharp):
    1. if(Event.current.control)
    2. {
    3.    //Ctrl key pressed.
    4. }
    5.  
    6. if(Event.current.shift)
    7. {
    8.    //Shift key pressed.
    9. }
    You may not need to extend the editor, since any pre-existing OnGUI functionality will populate the
    Event.current
    reference, but if you are making a custom editor, you will have to use some kind of OnGUI-type method to listen for Events (OnGUI, OnSceneGUI, OnInspectorGUI, etc.). There's no way around it.
     
    Lekret likes this.
  3. homer_3

    homer_3

    Joined:
    Jun 19, 2011
    Posts:
    111
    When I tried using Event, like I mentioned in my 1st post, I didn't get any key events. Is there some special setup I need to use?

    If I make a monobehavior like this, I get no key events.

    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class DragClone : MonoBehaviour
    3. {
    4.     private void OnGUI()
    5.     {
    6.         Debug.Log(Event.current);
    7.     }
    8. }
    If I extend Editor instead and use OnSceneGUI, I also get no key events. It's mostly just repaint, layout, and mouse move events.
     
  4. MikeDanielsson

    MikeDanielsson

    Joined:
    Jul 6, 2018
    Posts:
    10
    Old post but unanswered.

    This is how I do it:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class YourClassName
    5. {
    6.         [UnityEditor.Callbacks.DidReloadScripts]
    7.         private static void ScriptsHasBeenReloaded()
    8.         {
    9.             SceneView.duringSceneGui += DuringSceneGui;
    10.         }
    11.  
    12.         private static void DuringSceneGui(SceneView sceneView)
    13.         {
    14.            Event e = Event.current;
    15.          
    16.            if (e.type == EventType.KeyUp)
    17.            {
    18.               //Do Something
    19.            }
    20.  
    21.            if (e.type == EventType.KeyDown)
    22.            {
    23.               //Do Something
    24.            }
    25.  
    26.            //Right mouse button
    27.            if (e.type == EventType.MouseDown && Event.current.button == 0)
    28.            {
    29.        //Do Something
    30.       }
    31.    }
    32. }
    - You need UnityEngine for events to work.
    - You need UnityEditor for SceneView.
    - Use any callback as [UnityEditor.Callbacks.DidReloadScripts].
    - The function (ScriptsHasBeenReloaded()) has to be static.
    - Subscribe with any function to SceneView.duringSceneGui. That function also needs to be static.
     
    Last edited: Feb 4, 2023