Search Unity

Button is hooked with the space key

Discussion in 'UI Toolkit' started by Ndrs24, Oct 6, 2021.

  1. Ndrs24

    Ndrs24

    Joined:
    Apr 27, 2021
    Posts:
    1
    How can I solve that?
     
  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Hi, in the editor all buttons handle the space key by default to remain consistent with the rest of the editor. If you want to change this behavior you can intercept the KeyDownEvent and stop the event propagation.

    Code (CSharp):
    1. button.RegisterCallback<KeyDownEvent>((evt) =>
    2. {
    3.         if (evt.keyCode == KeyCode.Space)
    4.             evt.StopPropagation();
    5. }, TrickleDown.TrickleDown);
    At runtime you can go in the input manager and change what is mapped to the "Submit" action.
     
  3. PlayCreatively

    PlayCreatively

    Joined:
    Jan 25, 2016
    Posts:
    94
    Your code didn't work. This works though:
    Code (CSharp):
    1. root.RegisterCallback<NavigationSubmitEvent>((evt) =>
    2. {
    3.     evt.StopPropagation();
    4. }, TrickleDown.TrickleDown);
    There's also a Cancel- and MoveEvent which might interest you.