Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity UI How to set Toggle UI Component to ignore Keyboard input?

Discussion in 'UGUI & TextMesh Pro' started by zevonbiebelbrott, Dec 19, 2022.

  1. zevonbiebelbrott

    zevonbiebelbrott

    Joined:
    Feb 14, 2021
    Posts:
    118
    I have a toggle that I only want to be interactable with the mouse, when the toggle is pressed, quickly afterwards the player will press Spacebar to perform another action, but this toggles the Toggle instead aswell!

    I think its because it was just "selected", because pressing somewhere else in the game view, will "deselect" it and spacebar will then no longer toggle it.

    Is there a hacky? way to immediately "deselect" the toggle after its clicked with the mouse to quickly and a bit dirtly fix this issue? I dont want to dig into InputModule etc if its unnecesaary for this prototype.

    Edit: Completely disabling Keyboard input on UI elements would also be an accepted solution, please advice.

    Thank you/
     
  2. zevonbiebelbrott

    zevonbiebelbrott

    Joined:
    Feb 14, 2021
    Posts:
    118
    I tried digging into the StandaloneInputModule.cs to disable Keyboard input from there, but it seems impossible in the current version without rewriting a lot of code in that class, as it seemed to me it was doing mouse and keyboard "Submits" at the same time, so you could only enable/disable them together without rewriting the entire loop.

    I solved it by unchecking "Send Navigation Events" in the EventsSystem GameObject though, mouse is still working, just keyboard input is disabled on all UI now it seems, which is a good solution for now.
     
  3. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    595
    Depending on your situation an alternative to completely disabling "Send Navigation Events" is preventing the button from being selected. Input module can't press the button using navigation events if the button isn't selected. Setting navigation mode to None should prevent selectable from being selected when clicking it with mouse as can be seen looking at following part of UGUI Selectable (base class of most UGUI interactable elements) source code.

    Code (CSharp):
    1.  
    2. public virtual void OnPointerDown(PointerEventData eventData)
    3. {
    4.     if (eventData.button != PointerEventData.InputButton.Left)
    5.         return;
    6.  
    7.     // Selection tracking
    8.     if (IsInteractable() && navigation.mode != Navigation.Mode.None && EventSystem.current != null)
    9.         EventSystem.current.SetSelectedGameObject(gameObject, eventData);
    10.  
    11.     isPointerDown = true;
    12.     EvaluateAndTransitionToSelectionState();
    13. }
    14.  
    That way you can prevent selection of some specific buttons (or other selectable ugui elements) while maintaining keyboard navigation in other places.
     
    zevonbiebelbrott likes this.
  4. zevonbiebelbrott

    zevonbiebelbrott

    Joined:
    Feb 14, 2021
    Posts:
    118

    Very cool! Thank you.