Search Unity

Listening to key events on the entire editor window

Discussion in 'UI Toolkit' started by TJHeuvel-net, Aug 18, 2020.

  1. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    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:

    Code (csharp):
    1.  
    2.         rootVisualElement.RegisterCallback<KeyDownEvent>(kd =>
    3.         {
    4.             Debug.Log($"{kd} cmd: {kd.commandKey}");
    5.  
    6.             if(kd.keyCode == KeyCode.V && kd.commandKey)
    7.             {
    8.                 Debug.Log("PASETE");
    9.             }
    10.  
    11.         }, TrickleDown.TrickleDown);
    12.  
    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.

    upload_2020-8-18_12-21-6.png
     
    Last edited: Aug 18, 2020
  2. chris-z562

    chris-z562

    Joined:
    Jul 11, 2012
    Posts:
    41
    Your root is likely shrinking to the size it needs for its children. Try this:

    rootVisualElement.style.flexGrow = 1f;
     
    TJHeuvel-net likes this.