Search Unity

tabbing between fields

Discussion in 'Getting Started' started by boolfone, Jun 29, 2016.

  1. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    I’m working on a Unity form over here for people to sign up for a newsletter:

    http://coolfone.mobi/EmailSubscribe/

    Screen Shot 2016-06-29 at 6.26.38 PM.png

    I’d like the user to be able to tab from the Name field to the Email field and vice versa.

    Anyone know an easy way to do this?

    Thanks.
     
  2. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    Here's pseudo code to get you started...this code would be attached to the input field.

    Code (CSharp):
    1. EventSystem myEventSystem;
    2.  
    3. if (input.GetKey(KeyCode.Tab) {
    4. currentInputField = myEventSystem.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown;
    5. }
    This will go to the next UI object in the event system. (If I recall correctly). Sorry that's the best I can do for now.
     
    Last edited: Jun 30, 2016
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I don't think you need any code for this... it's what the navigation options are for. I would try those first.
     
    MikeTeavee likes this.
  4. Gmotagi

    Gmotagi

    Joined:
    Jul 27, 2013
    Posts:
    13
    Trouble is that its work with tabs (only arrow keys), so if you want to tab between them you will need to implement it manually.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Oh I see. I rarely use keyboard navigation, so I didn't realize this!

    It seems like quite an oversight... tab to advance and shift-tab to go back seem like such standards, I'm surprised people aren't screaming about this more. At the very least, somebody ought to file a bug report.
     
  6. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    JoeStrout likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The navigation works on the Input axes. Specifically horizontal and vertical. Just put the tab key in there.

    It's a little annoying that they choose the same input axes as the default movement ones. So just be aware or you'll find tab also moves you character around.
     
    Ryiah and JoeStrout like this.
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    But actually, you can change these axes in the EventSystem properties (the Standalone Input Module component). So I guess you could define separate axes just for keyboard navigation. Though I suspect that their mixing of horizontal and vertical axes (instead of a single next/previous control axis) might make for a somewhat awkward fit, if you have both horizontal and vertical scrollers in your UI.
     
    Ryiah and Kiwasi like this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Well that does simplify things.

    Another trick that's worth noting for this stuff is you can define the same axis multiple times in the Input Manager. Unity will check all of them. That way you can assign tab and down and right all to the same axis.

    Still not sure how you could appply multiple key presses. So Shift+Tab is probably not supportable.
     
  10. Callidis

    Callidis

    Joined:
    May 25, 2018
    Posts:
    1
    For future readers:

    Here is a sample of a navigation script I wrote that supports tabbing and reverse tabbing(tab+left or right shift).

    I attached this script to my EventSystem which was created with the Canvas that houses my login menu.

    Note: I added a constants class which has constants that refer to the Game Object names of my three login controls. I am sure this isn't the "best" way to do it, but I had a hard time finding any examples so hope this help.

    Don't forget: (using UnityEngine.EventSystems to reference EventSystem)

    Code (CSharp):
    1. private EventSystem myEventSystem;
    2.  
    3. void Update()
    4.     {
    5.         if(
    6.             (Input.GetKeyDown(KeyCode.Tab) && Input.GetKey(KeyCode.LeftShift)) ||
    7.             (Input.GetKeyDown(KeyCode.Tab) && Input.GetKey(KeyCode.RightShift))
    8.             )
    9.         {
    10.             myEventSystem = GetComponent<EventSystem>();
    11.             switch (myEventSystem.currentSelectedGameObject.name)
    12.             {
    13.                 case LoginControls.INPUT_EMAIL:
    14.                     myEventSystem.SetSelectedGameObject(GameObject.Find(LoginControls.BUTTON_LOGIN));
    15.                     break;
    16.                 case LoginControls.INPUT_PASSWORD:
    17.                     myEventSystem.SetSelectedGameObject(GameObject.Find(LoginControls.INPUT_EMAIL));
    18.                     break;
    19.                 case LoginControls.BUTTON_LOGIN:
    20.                     myEventSystem.SetSelectedGameObject(GameObject.Find(LoginControls.INPUT_PASSWORD));
    21.                     break;
    22.                 default:
    23.                     myEventSystem.SetSelectedGameObject(GameObject.Find(LoginControls.INPUT_EMAIL));
    24.                     Debug.Log("default hit for shift tab");
    25.                     break;
    26.             }
    27.         }
    28.         else if (Input.GetKeyDown(KeyCode.Tab))
    29.         {
    30.             myEventSystem = GetComponent<EventSystem>();
    31.             switch (myEventSystem.currentSelectedGameObject.name)
    32.             {
    33.                 case LoginControls.INPUT_EMAIL:
    34.                     myEventSystem.SetSelectedGameObject(GameObject.Find(LoginControls.INPUT_PASSWORD));
    35.                     break;
    36.                 case LoginControls.INPUT_PASSWORD:
    37.                     myEventSystem.SetSelectedGameObject(GameObject.Find(LoginControls.BUTTON_LOGIN));
    38.                     break;
    39.                 case LoginControls.BUTTON_LOGIN:
    40.                     myEventSystem.SetSelectedGameObject(GameObject.Find(LoginControls.INPUT_EMAIL));
    41.                     break;
    42.                 default:
    43.                     myEventSystem.SetSelectedGameObject(GameObject.Find(LoginControls.INPUT_EMAIL));
    44.                     Debug.Log("default hit for normal tab");
    45.                     break;
    46.             }
    47.         }
    48.     }
    49.  
    50.     public static class LoginControls
    51.     {
    52.         public const string INPUT_EMAIL = "EmailAddressInput";
    53.         public const string INPUT_PASSWORD = "PasswordInput";
    54.         public const string BUTTON_LOGIN = "LoginButton";
    55.     }
     
  11. unity_LHUd_QtU8oCfpg

    unity_LHUd_QtU8oCfpg

    Joined:
    Sep 22, 2018
    Posts:
    1
    here is my code. its in danish, but i have tried to describe what that is doss.
    its work fine.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System;
    5. using UnityEngine;
    6. using UnityEngine.EventSystems;
    7.  
    8. public class TabScript : MonoBehaviour
    9. {
    10.  
    11.     public GameObject[] indputvælg; // a array for selecting gameobjekts
    12.  
    13.     public GameObject førsteValg;   // the first selected gameobjekt
    14.  
    15.     public GameObject valgt;        // current Selected GameObject
    16.  
    17.     public int index;               // a int for the array
    18.  
    19.     private void Start()
    20.     {
    21.         // set the first gameobjekt
    22.         EventSystem.current.GetComponent<EventSystem>().firstSelectedGameObject = førsteValg;
    23.         EventSystem.current.GetComponent<EventSystem>().SetSelectedGameObject(førsteValg);
    24.         print("længde " + indputvælg.Length);
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         // gets the gameobjekt that is chosen
    30.         valgt = EventSystem.current.GetComponent<EventSystem>().currentSelectedGameObject;
    31.  
    32.         if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
    33.         {
    34.             print("shiift trykket");
    35.             if (Input.GetKeyDown(KeyCode.Tab) || Input.GetKeyDown(KeyCode.Tab))
    36.             {
    37.                 print("tab trykket");
    38.  
    39.                 hentliste();
    40.                 index--;
    41.  
    42.                 if (index >= 0)
    43.                 {
    44.                     print(" 2 " + (indputvælg.Length));
    45.                     EventSystem.current.GetComponent<EventSystem>().SetSelectedGameObject(indputvælg[index]);
    46.                 }
    47.             }
    48.         }
    49.  
    50.         else
    51.  
    52.             if (Input.GetKeyDown(KeyCode.Tab))
    53.         {
    54.      
    55.             hentliste();
    56.             index++;
    57.             if (index < (indputvælg.Length))
    58.             {
    59.                 print(" 2 " + (indputvælg.Length));
    60.                 EventSystem.current.GetComponent<EventSystem>().SetSelectedGameObject(indputvælg[index]);
    61.             }
    62.         }
    63.     }
    64.     // sets the index for the chosen gameobjekt
    65.     public void hentliste()
    66.     {
    67.         for (int i = 0; i < indputvælg.Length; i++)
    68.         {
    69.             if (EventSystem.current.GetComponent<EventSystem>().currentSelectedGameObject.name == indputvælg[I].name)
    70.             {
    71.                 index = i;
    72.                 print("index " + i);
    73.             }
    74.         }
    75.     }
    76. }
    77.  
    [/I]
     
    Last edited: Sep 22, 2018