Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AwesomeX · Sep 09, 2014 at 06:59 PM · uiguiinputfocuschat

Focus on InputField upon key press?

Basically the title, I'm trying to focus on an Input Field, when a player presses a key.

I'm using this for a chatbox, so I want it when someone presses ENTER or Y, the chat will pop up, and focus so they can type, then they can press ENTER again to send it. I've got the chat box working already, just wondering how I can setup the focusing and what not.

I'm currently using a work around. Here's my script.

     using UnityEngine;
     using UnityEngine.UI;
     using UnityEngine.EventSystems;
      
     public class Chat : BoltCallbacks
     {
         public Scrollbar scroll;
      
         public RectTransform content;
         public Text chatLine;
         public InputField input;
      
         public override bool PersistBetweenStartupAndShutdown ()
         {
             return true;
         }
      
         void Awake ()
         {
             input.onSubmit.AddListener (SendChat);
         }
      
         void Update ()
         {
             scroll.value = + -0.01f * Time.deltaTime;
      
             if (Input.GetKeyDown(KeyCode.Y))
             {
                 input.gameObject.SetActive(true);
                 EventSystemManager.currentSystem.SetSelectedGameObject(input.gameObject, null);
                 input.OnPointerClick (null);
             }
         }
        
         void SendChat (string line)
         {
             input.value = "";
             input.gameObject.SetActive (false);
      
             if (line == "")
             {
                 return;
             }
      
             IChat evnt = BoltFactory.NewEvent<IChat>();
             evnt.text = PlayerNamePlate.playerName + ": " + line;
             BoltNetwork.Raise(evnt);  
         }
      
         public override void OnEvent (IChat evnt, BoltConnection cn)
         {
             Text newLine = ((GameObject) Instantiate(chatLine.gameObject)).GetComponent<Text>();
             newLine.gameObject.SetActive(true);
             newLine.rectTransform.parent = content;
             newLine.text = evnt.text;
         }
     }
 

The problem now is, whenever I open chat with "Y", the chatbox actually types Y.. I've tried a few things, nothing seems to stop from typing the first Y, which is just suppose to open the chat.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by orb · Sep 09, 2014 at 08:10 PM

If you use Input.GetKeyUp() instead it'll avoid that. It's usually the more appropriate check to use for one-off actions anyway.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AwesomeX · Sep 10, 2014 at 12:27 AM 0
Share

That doesn't solve the problem at all.

Thanks anyway.

avatar image
0

Answer by HaydenHuff · Sep 30, 2014 at 02:56 AM

If it adds an extra y every time, it might be possible to manually remove it every time, or at the very least set the text to "" once before the player can hit a second key.

I actually found this while looking for a way to make a function manually select/focus an InputField fully, where the next key entered is written to the field. I've managed to use SetSelectedGameObject to set the InputField as selected, but this was only the state that allowed for keyboard navigation to move between different UI elements. From your post it seems I also need

inputField.OnPointerClick (null);

Thanks for the help, and good luck with your project.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by MIHGames · Sep 30, 2014 at 03:51 AM

Try using WaitForSeconds() For example:

 if (Input.GetKeyUp(KeyCode.Y))
             {
 
                 yield return new WaitForSeconds(1);
                 input.gameObject.SetActive(true);
                 EventSystemManager.currentSystem.SetSelectedGameObject(input.gameObject, null);
                 input.OnPointerClick (null);
             }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make a "move pad" on the screen? (Mobile) 1 Answer

TextField, Event.current, Input.GetKey, and GUI.FocusControl locking 1 Answer

How can I release gui focus via code? 6 Answers

Why when set TouchScreenKeyboard.hideInput = true is not supported by any language besides English? 0 Answers

Defocus input line? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges