- Home /
Press Return in TextFiled to press button
Ok i have asked this question before but i couldn't give good enough information because work was closing at the time and i needed to leave.
My Unity has been bugged and the previous question i asked on this subject (the one im editing over) i do not believe was a good question so sorry about that.
I am having issues getting my Login form to work correctly. I have a TextField, a PasswordField and a GUI.Button.
mu code basically runs if textField = "user" && passwordField = "pass" loadLevel(1)
I simply want it so if i press Return from inside the passwordField then it will press the button.
I have looked into this matter quite a bit and from what i can tell a simple OnKeyDown will not work. I have also tried the Event.Current system and although it does have an output I'm not sure how i get that to activate my button.
Please note: in the following code i use to have the Event.Current in the OnGUI function but moved it after it wasn't showing in the console while selecting text.
private static int buttonHeight = (Screen.height/10); private static int buttonWidth = (Screen.width/3); private string u = "AceScottie"; private string p = "password99";
private string usernameString = string.Empty;
private string passwordString = string.Empty;
private Rect windowsRect = new Rect(0, 0, Screen.width, Screen.height);
void OnGUI()
{
GUI.Window (0, windowsRect, WindowFunction, "Login");
GUI.skin.textField.fontSize = Screen.height/15;
}
void WindowFunction (int WindowID) {
usernameString = GUI.TextField (new Rect (Screen.width / 3, 2 * Screen.height / 5, Screen.width / 3, Screen.height / 10), usernameString, 16);
passwordString = GUI.PasswordField (new Rect (Screen.width / 3, 2 * Screen.height / 5 + buttonHeight, Screen.width / 3, Screen.height / 10), passwordString, "*" [0], 16);
Event EReturn = Event.current;
if (EReturn.isKey && EReturn.keyCode == KeyCode.Return)
Debug.Log ("Hit return!");
if (GUI.Button (new Rect (Screen.width / 3, 2 * Screen.height / 5 + buttonHeight + buttonHeight, buttonWidth, buttonHeight), "Login")) {
if (usernameString == u && passwordString == p) {
Application.LoadLevel ("MainGUI");
} else {
Debug.Log ("Wrong Username or passoword");
passwordString = "";
}
}
}
void Update () {
if(Input.GetKeyDown(KeyCode.Escape)) {
Application.Quit();
}
}
}
since 41 people have seen this and still no answer im going to assume its either a really hard thing to do, or im being stupid and missing some basic fundamental step. Either way can someone point me in the right direction on this ? like just a general clue...
Your answer
![](https://koobas.hobune.stream/wayback/20220613190743im_/https://answers.unity.com/themes/thub/images/avi.jpg)