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 Setzer22 · Dec 20, 2012 at 06:49 PM · guigetkeydown

Input.GetKeyDown not working properly on a GUI textbox

Hello to everyone.

This is an issue I've come across many times while using Unity. Long story short, the Input.GetKeyDown function is not always working. To be more specific, I'm trying to do this:

I'm trying to make a simple commands console to help me into developing my game, this is what I've got so far (Just started as you can see). I've marked with a comment the three lines I want to show:

     void Update() {
 
         if(Input.GetKeyDown (KeyCode.F5)) toggleGUI = !toggleGUI;
         if(GUI.GetNameOfFocusedControl() == "console") {
             Debug.Log (Input.GetKeyDown(KeyCode.F6));//HERE
             if(Input.GetKeyDown(KeyCode.F6)) Debug.Log ("PRESSED A KEY!");//HERE
             if(Input.GetButtonDown("Fire1")) Debug.Log ("PRESSED BUTTON!");//AND HERE
         }
     }
     
     void OnGUI() {
         if (toggleGUI) {
             console_rectangle = GUI.Window (2,console_rectangle,MakeConsole,"Developer console");    
         }
     }


So I want it to behave as follows:

  • Whenever I press F5, the console window should be open (this works)

  • While the console command text box being open and active, if I press F6, i want the text in the conmmand box to be processed (here is the problem)

As you can see, those Debug.Log lines are there because this is not actually working as it should. The Input.GetKeyDown is working fine for F5, but neither the first Debug.Log nor the second I marked with the comments are working.

The strange issue I have is that the third Debug.Log, is working whever I click my mouse without a problem.

So, to summarize, looking into my code, in the Update function, GetKeyDown is working for F5 outside the if statement, but not working at all for F6 (i'm not getting any output in the console) inside the if statement. And also the GetButton function is working just fine whenever I use it.

It's not the first time i come across an issue like this. Most of the time I solved it by adding the keys I wanted as avaliable input buttons for GetButton, which is what I would want to do in order to release my game anyway, but this is really bothering me, and I haven't found anyone with the same problem.

Any help would be much appreciated.

Thank you very much!

IMPORTANT EDIT: Ok, this might be what is causing me trouble, I've found out that no key (neither using GetKey nor using GetButton works while a textBox is active, but the mouse does work, rather than being the method not working. So taking that into account I'm still interested into accessing keyboard events while the texbox is active. I could add a GUI button as a last resource, but I feel it's easier to just press enter.

Comment
Add comment · Show 3
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 PAEvenson · Dec 20, 2012 at 07:04 PM 0
Share

what does it say when you Debug.Log(GUI.GetNameOfFocusedControl()) before the if(GUI.GetNameOfFocusedControl() == "console") after you hit F5

avatar image JaydenM1997111 · Dec 20, 2012 at 07:13 PM 0
Share

After calling an 'if' statment, remember to use '{' and '}' EG:

if(GUI.GetNameOfFocusedControl() == "console") { //code in here }

avatar image Setzer22 · Dec 20, 2012 at 09:52 PM 0
Share

Ok, I tested it. Everything seems to be working as it should, the expression (GUI.GetNameOfFocusedControl() == "console") evaluates true only when the text box of the console is active and in any other situation evaluates as false.

@$$anonymous$$den$$anonymous$$1997111 You're right, it's more clear if I use the parentheses "{" "}" (I'm not sure how are they called) and I should use them (I'm a bit lazy though xD). But anyway I don't think that could cause any error in the code as the two if statements are independant from each other, and as the first one is a single-line one the compilator understands it.

EDIT (I posted this message before the one above, but didn't want to double post):

"Well, I did some previous testing after posting this and right now I can tell you GetNameOfFocusedControl equals "console" when it should, and the (let's call it) inside of the if statement is reached as the GetButton method works fine. But anyway I'm going to do as you say and post with feedback, right now I'm not at home but when I go back tonight i'll test it and post it."

0 Replies

· Add your reply
  • Sort: 

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

12 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

Related Questions

How does unity focus GUI when different keys are pressed? 0 Answers

How to make a GUI appear and disappear with the same button? 1 Answer

OnGUI Button 1 Answer

Making iTween work with OnGUI functions 3 Answers

Unity OnGUI elements have offset touch detections 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