- Home /
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
Answer by MakinStuffLookGood · Jan 21, 2014 at 05:08 AM
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 http://docs.unity3d.com/Documentation/ScriptReference/Event.html for more info on the Event class.
Thank you for this, it worked I just needed to modify the following.
if (e.is$$anonymous$$ey && e.keyCode = $$anonymous$$eyCode.Return)
to
if (e.is$$anonymous$$ey && e.keyCode == $$anonymous$$eyCode.Return)
Ahh of course, sorry I just edited a code snippet from the unity docs. Updated answer to reflect that correction.
Your answer
Follow this Question
Related Questions
Checking whether string is a valid Input.Key 1 Answer
Can § be used as input? 3 Answers
Issues with Input.inputString 2 Answers
Detect from which keyboard key was pressed and cancel event 0 Answers