Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    59
    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:
    292
    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:
    59
    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:
    292
    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.