Press GUI button with key press

Hi there,

I am trying to get this little bit to work. All of my scripting works but I wanted it to press the login button when I press return, but for some reason it just doesn’t work. if I add a print to the key press it shows up.

if (GUILayout.Button ("Login") || Input.GetKeyDown(KeyCode.Return))
			{
				if (username == "" || _password == "")
				{
					_report += "Both username and password required";
				}
				else
				{
					WWWForm form = new WWWForm();
					form.AddField("Username", username);
					form.AddField("Password", _password);
					form.AddField("secretKey", _secretKey);
					WWW w = new WWW(loginAccount, form);
					StartCoroutine(register(w));

				}
			}

Can anyone shed any light on the possible issue please?

Thanks,

Doomie

I imagine this is happening inside OnGUI?

You cannot use the Input class inside OnGUI, you can however, use the Event class. Something like this:

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    void OnGUI() {
        Event e = Event.current;
        if (e.isKey && e.keyCode == KeyCode.Return)
            Debug.Log("Hit return!");
    }
}

See Unity - Scripting API: Event for more info on the Event class.