- Home /
Input.GetKeyDown bug introduced with 3.4?
I have a simple chat client project that would build and run with no issues in Unity 3.3. I recently installed Unity 3.4, and went back to the project today. It builds and runs, but I noticed something strange - Input.GetKeyDown does not seem to be working at all for me. Here is the code:
void Update()
{
if (Input.GetKeyDown("return"))
{
SendChatMessage();
}
}
SendChatMessage() is never called. I added Debug.Log() statements immediately before the Input check, and within the if block - Update() is being called properly, but the GetKeyDown call never returns true.
I've tried Input.GetKeyDown(KeyCode.Return) and Input.GetKeyDown("enter"). Neither work. I even tried some other keys - none of them respond to keyboard input.
Again, this same code worked properly in 3.3. AFAIK, absolutely nothing has changed. Any ideas?
Sorry, should have mentioned that I tried that too. Doesn't work.
use keycode void Update() { if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.return)) { SendChat$$anonymous$$essage(); } }
Answer by rbbrdckybk · Aug 04, 2011 at 06:46 PM
I just found the issue by commenting out code piece by piece. For some reason, when I commented out my OnGUI() method, input started working again.
I do have some async socket calls (it's a chat client), and I know some wonky things can happen if you're not careful to leave Unity objects alone outside of the main thread, so I assume that I wasn't careful enough and the issue is related to threading somehow.
Odd that this worked perfectly in 3.3, though.
The issue actually ended up being that Input.Get$$anonymous$$eyDown no longer works when a GUI.Textfield has focus in Unity 3.4. In 3.3, that was not the case. Wish changes like this were documented.
That's actually great news :D I've missed that feature all the time. You always needed some kind of workaround to detect that you're focusing a textfield and manually "ignore" the normal input.
Answer by Bunny83 · Aug 04, 2011 at 08:54 PM
I've just looked up the Update news and i've found the change ;)
Input.eatKeyPressOnTextFieldFocus
That's what you need ;)
It's even already in the documentation: Input.eatKeyPressOnTextFieldFocus
See the update page / Other Improvements (near the end)
Input.eatKeyPressOnTextFieldFocus added to be able to control input behavior during textfield focus. This is default true from 3.4 and forth. Set this to false to achieve pre 3.4 behavior.
Ah, thanks - I completely missed that. That actually makes a lot of sense. $$anonymous$$y workaround was going to be something like this in OnGUI():
Event e = Event.current;
if (e.is$$anonymous$$ey && e.keyCode == $$anonymous$$eyCode.Return)
{
SendChat$$anonymous$$essage();
}
... but your solution is better.
If the Textfield is in OnGUI it actually makes more sense to use the Event class. Your "workaround" is actually the best solution ;).
The Input class is for game input and only works in Update.
btw. is$$anonymous$$ey is true for both : $$anonymous$$eyup and $$anonymous$$eydown so your function will be executed twice.
if (e.type == EventType.$$anonymous$$eyDown && e.keyCode == $$anonymous$$eyCode.Return)
{
SendChat$$anonymous$$essage();
}
that would be the equivalent to Input.Get$$anonymous$$eyDown
Answer by Graham-Dunnett · Aug 04, 2011 at 06:50 PM
This worked for me:
//javascript
var s : String = "hello";
function OnGUI () {
if (GUI.Button(Rect(10,70,150,30),s)) {
s = "world";
}
}
function Update () {
if (Input.GetKeyDown("return"))
{
s = s + "x";
}
}
Your answer