Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Custom Editor: Use X, F, etc.. as Shortcut?

Discussion in 'Extensions & OnGUI' started by ModLunar, Jul 27, 2018.

  1. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    65
    There are already pre-defined keyboard shortcuts for using Unity, like Z switches pivot vs. center point, X changes between local and global space axes, F frames in on an object, etc.
    In short, my question is: In an editor script, can I use a shortcut that Unity already uses (like F) but for my own purpose?

    ---

    Here are the details: so I have a custom Editor class (inheriting from UnityEditor.Editor) that I would like to have some keyboard shortcuts for. Being GUI code in the editor, I use something like this in the Editor's OnInspectorGUI():

    Code (CSharp):
    1. public override void OnInspectorGUI() {
    2.     Event e = Event.current;
    3.     switch (e.type) {
    4.         case EventType.KeyDown:
    5.             switch (e.keyCode) {
    6.                 case KeyCode.F:
    7.                     //F was just pressed down!
    8.                     //Do some custom thing I want to do here
    9.                     e.Use();
    10.                     break;
    11.             }
    12.             break;
    13.     }
    14. }
    So the thing is -- this works. But, because F is a keyboard shortcut already used in the editor to focus on an object in the Scene View, it also does that, moving my Scene View camera when I don't want it to.

    I thought using e.Use() would prevent the event from propagating any further (so it's only registered that F was pressed once, used up by my custom code, and not also cause a frame-in on an object)

    Is there any way I can still use F without triggering Unity's default keyboard shortcut F functionality?
     
    Last edited: Jul 27, 2018
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    493
    That shouldn't be happening unless the SceneView or Hierarchy is focused when you press F. If you've focused the inspector then F shouldn't be doing anything in the SceneView or Hierarchy.
     
  3. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    65
    Ah yeah you're right, it's cause I use SceneView.onSceneGUIDelegate as well for some more interactive input-based editing tools, so pressing F there does it. So I'm still wondering how to get around it if possible. (For now I'm just using different, unused shortcut keys)
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    493
    Simply "Using" the event I want to override the behaviour of should be enough to cancel the default operation. I've done this myself to great success in the past:

    Code (CSharp):
    1. if( evt.type == EventType.KeyDown ) {
    2.     if( evt.keyCode == KeyCode.F ) {
    3.         // do something on f
    4.         evt.Use();
    5.     }
    6. }
    This worked as intended for me in a new project, though once the SceneView was unfocused and wasn't receiving key events the global behaviour understandably took over again.

    If you want to be very certain, I recommend also setting the hotControl and/or keyboardControl where it makes sense to, as there are a few operations (like object selection with the mouse) that can only be overridden by resetting the hotControl before MouseUp.