- Home /
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.
what does it say when you Debug.Log(GUI.GetNameOfFocusedControl()) before the if(GUI.GetNameOfFocusedControl() == "console") after you hit F5
After calling an 'if' statment, remember to use '{' and '}' EG:
if(GUI.GetNameOfFocusedControl() == "console") { //code in here }
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."
Your answer
