MenuItem hotkeys and naming objects

[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?

bump

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.

Of course they thought about it, you have not searched enough!
Try something like that:

    MenuItem("Hotkeys/Step _v")]
    public static void Test()
    {
        // Checks that a textfield are not focused
        if(GUIUtility.keyboardControl == 0)
        {
            // Put your code here
        }
    }

You are welcome.

2 Likes

I have no idea where you find that kind of stuff! :wink:
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.

using System;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad, ExecuteInEditMode]
public class ProgrammerHotkeys : MonoBehaviour
{
    [MenuItem("Hotkeys/Test _v", false, int.MaxValue)]
    public static void Update()
    {
        if (GUIUtility.keyboardControl == 0) {
            Debug.LogError ("Accepting hotkey");
        } else {
            Debug.Log("Not accepting hotkey");
        }
    }
}

Here’s a gif showing how it doesn’t work when I click on V:

MenuItem("Hotkeys/Step _v")]
    public static void Test()
    {
        // Checks that a textfield are not focused
        if(GUIUtility.keyboardControl == 0)
        {
            // Put your code here
        }
    }

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:

MenuItem("Hotkeys/Step _v")]
    public static void Test()
    {
             if (EditorGUIUtility.editingTextField) return;
             EditorApplication.ExecuteMenuItem("Edit/Play");
    }

The letter gets “eaten” by this [MenuItem(“Hotkeys/PlayTest _v”)]