Search Unity

MenuItem hotkeys and naming objects

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Jul 16, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    [MenuItem("Hotkeys/Step _v")] or [MenuItem("Hotkeys/Step #v")]

    How can I use hotkeys like 'v' or 'V' without the hotkeys being called while I'm editing text within Unity's editor?
     
  2. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Did the guys from Unity really not think of something that basic?
    Calling the hotkeys while editing text seems like something that would happen to pretty much anyone renaming or naming a file.
     
  4. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Of course they thought about it, you have not searched enough!
    Try something like that:
    Code (csharp):
    1.  
    2.     MenuItem("Hotkeys/Step _v")]
    3.     public static void Test()
    4.     {
    5.         // Checks that a textfield are not focused
    6.         if(GUIUtility.keyboardControl == 0)
    7.         {
    8.             // Put your code here
    9.         }
    10.     }
    11.  
    You are welcome.
     
    Johannski and carl010010 like this.
  5. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I have no idea where you find that kind of stuff! ;)
    It's not working properly for me however, I still can't write v when renaming a file and it seems to randomly do the if or the else.

    Code (CSharp):
    1. using System;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. [InitializeOnLoad, ExecuteInEditMode]
    6. public class ProgrammerHotkeys : MonoBehaviour
    7. {
    8.     [MenuItem("Hotkeys/Test _v", false, int.MaxValue)]
    9.     public static void Update()
    10.     {
    11.         if (GUIUtility.keyboardControl == 0) {
    12.             Debug.LogError ("Accepting hotkey");
    13.         } else {
    14.             Debug.Log("Not accepting hotkey");
    15.         }
    16.     }
    17. }

    Here's a gif showing how it doesn't work when I click on V:
    http://i.imgur.com/V1cBO2C.gifv
     
    Last edited: Aug 14, 2017
  6. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Code (CSharp):
    1. MenuItem("Hotkeys/Step _v")]
    2.     public static void Test()
    3.     {
    4.         // Checks that a textfield are not focused
    5.         if(GUIUtility.keyboardControl == 0)
    6.         {
    7.             // Put your code here
    8.         }
    9.     }
    10.  
    That doesn't work but the code below at least works in the sense that it doesn't use the hotkey while editing a text field, HOWEVER it still won't write the letter in the text field:
    Code (CSharp):
    1. MenuItem("Hotkeys/Step _v")]
    2.     public static void Test()
    3.     {
    4.              if (EditorGUIUtility.editingTextField) return;
    5.              EditorApplication.ExecuteMenuItem("Edit/Play");
    6.     }
    7.  
    The letter gets "eaten" by this [MenuItem("Hotkeys/PlayTest _v")]