- Home /
Closing Textfield via Input.GetKeyDown not working under MacOSX(No problem under Windows)
I use the attached code to load a level on Input.GetKeyDown, but it is not working under MacOSX, under Windows it is working just fine!
I guess it has to do with the Textfield, because closing a normal Gui.Button with Input.GetKeyDown is working fine, but I was not able to narrow it down.
var Username = "";
//
function ShowCustomNameWindow (windowID : int)
{
//
GUI.Label (Rect (100,100,300,45), "ENTER YOUR NAME:");
//
GUI.SetNextControlName ("Username");
GUI.FocusControl ("Username");
Debug.Log(GUI.GetNameOfFocusedControl);
//
Username = GUI.TextField (Rect (142,142,178,35), Username,11);
//
if(Input.GetKeyDown(KeyCode.Return) && Username.Length != 0
||Input.GetKeyDown(KeyCode.KeypadEnter) && Username.Length != 0)
//
{
//
Application.LoadLevel("xyz");
}
}
I don't know if it's related, but you should stay away from the Input classes in OnGUI code, and use Event.current ins$$anonymous$$d.
Uhhh!? ...good to know about that! I´ll try Event.current out now.
When I use a simple "Debug.Log("Current detected event: " + Event.current)" in this function, the returned keyCode output is None(But only for Return & Enter, normal keys are returning their valid keyCode output). Using this Debug.Log in normal OnGUI function the returned keyCode output for Enter & Retun is is correct. Any ideas?
Try checking the input before the textfield code, since it's probably eating the return key event.
Yes, that is true the textfield was eating the event! But when putting the Event code before the textfield, I was not longer able to control Username.Length correctly. So I came up with (Event.current.Equals(Event.$$anonymous$$eyboardEvent ("[enter]")) which works just fine also after the textfield.
Answer by GameGuy · Feb 06, 2012 at 09:04 PM
First of all Input.GetKeyDown should not be used in OnGui functions, for detecting any key use Event.current.
So I changed the function to :
var Username = "";
//
function ShowCustomNameWindow (windowID : int)
{
//
GUI.Label (Rect (100,100,300,45), "ENTER YOUR NAME:");
//
GUI.SetNextControlName ("Username");
GUI.FocusControl ("Username");
Debug.Log(GUI.GetNameOfFocusedControl);
//
Username = GUI.TextField (Rect (142,142,178,35), Username,11);
//
if((Event.current.Equals(Event.KeyboardEvent ("[enter]"))&& Username.Length != 0)
//
{
//
Application.LoadLevel("xyz");
}
}
Basically Input.GetKeyDown(KeyCode.KeypadEnter) turned into (Event.current.Equals(Event.KeyboardEvent ("[enter]"))
Thanks to Eric5h5, who helped me to find this solution!
Answer by unimechanic · Jul 03, 2013 at 04:14 PM
This example might help too:
http://forum.unity3d.com/threads/188202-Simple-dynamic-list-editor