Listening to key events on the entire editor window

I’m trying to implement paste functionality by detecting wether the user pressed Ctrl-V in an editor window. I’m trying to following just after creating the UI:

        rootVisualElement.RegisterCallback<KeyDownEvent>(kd =>
        {
            Debug.Log($"{kd} cmd: {kd.commandKey}");

            if(kd.keyCode == KeyCode.V && kd.commandKey)
            {
                Debug.Log("PASETE");
            }

        }, TrickleDown.TrickleDown);

However the callback is not fired at all, and i’m not sure why. I tried the manual’s example that listens to MouseDownEvent, and it is only fired when i click inside an area with ui elements, and not in the area at the bottom. Why is that, when i listen to the root element i expect every click to be contained? Just like in Javascript when you listen to an event on the document body it contains everything.

Your root is likely shrinking to the size it needs for its children. Try this:

rootVisualElement.style.flexGrow = 1f;

1 Like