Search Unity

How to get KeyDownEvent to work.

Discussion in 'UI Toolkit' started by Foxaphantsum, Apr 8, 2020.

  1. Foxaphantsum

    Foxaphantsum

    Joined:
    Jul 5, 2013
    Posts:
    139
    First I apologize for all the threads as of recent.


    I'm using Unity 2019.3.8 and for some reason am having trouble getting KeyDownEvent's working on my elements.

    I was able to get the mouseEventDown working no problem but can't for the life of me get key downs working.

    An example of what I'm doing.

    I've experimenting setting the root pickingmode to and playing with the elements picking mode and Focus() calls.

    I'm very confused as to what is needed to get this working.

    Code (CSharp):
    1. root.pickingMode = PickingMode.Position;
    2.  

    Code (CSharp):
    1.  
    2. var bottom = root.Q<VisualElement>();
    3. bottom?.RegisterCallback<KeyDownEvent>((e) =>
    4. {
    5.       Debug.Log(e.keyCode);
    6. });
     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Keyboard events are sent to focusable elements that are in focus.

    A vanilla
    VisualElement
    is not focusable by default but can made so by setting
    VisualElement.focusable
    to
    true
    .

    To make them focused, the user has to click into it or tab to give it focus.
    You can force the focus when the element is ready:

    element.RegisterCallback<AttachToPanelEvent>(evt => element.Focus());
     
    salah-rashad and CyrilGhys like this.
  3. Foxaphantsum

    Foxaphantsum

    Joined:
    Jul 5, 2013
    Posts:
    139
    Sorry for the late response. I recently went about trying to get this to work and for some reason the element is constantly getting unfocused. despite calling Focus()


    The only way i was able to get the key to work was by having a MouseMove event triggered at the same time as pressing my keys.

    Is there any example demonstrating a quick and easy way to get a keyboard action working without the keys toggling other parts of the unity editor??
     
    t2g4 likes this.
  4. t2g4

    t2g4

    Joined:
    Nov 13, 2018
    Posts:
    46
    Same here, Key events just don't work.

    I created a custom editor window, and registered KeyUpEvent on its root visual element:
    Code (CSharp):
    1. this.rootVisualElement.RegisterCallback<KeyUpEvent>( evt => { Debug.Log( evt.keyCode ); });
    Nothing is happening. No matter if this window is focused or not.

    Also, I want to move my SceneView IMGUI input system to UITollkit, but it just doesn't work. SceneView is receiving key events (as I can see through Event Debugger) but my registered events do not run, I tried TrickleDown/NoTrickleDown, tried to register key events on a parent, parent.parent and etc. Nothing work.

    I agree with Anisoft, there's just no clear demonstration of how to force key events to work with UIToolkit. Pointer events work, ChangeValue, etc. work, but KeyUp/KeyDown does not.
     
    Last edited: Jan 28, 2023
  5. t2g4

    t2g4

    Joined:
    Nov 13, 2018
    Posts:
    46