- Home /
Can I change the keycode that clicks the focused Unity GUI button?
(Please read below and don't just suggest Event.current.keyCode. Thanks!)
If a Unity GUI button has keyboard focus and the player presses KeyCode.Space, it clicks the button. For example, if you add this to a scene and press Space, it will log "CLICKED!".
public void OnGUI() {
GUI.SetNextControlName("ClickMeControlName");
if (GUILayout.Button("Click Me")) Debug.Log ("CLICKED!");
GUI.FocusControl("ClickMeControlName");
}
Is it possible to change Unity GUI's default behavior to a different KeyCode, such as KeyCode.Return? I'd prefer this to watching Event.current.keyCode for Space (to Use it to prevent it from clicking the button) and Return to simulate a click.
Answer by TonyLi · Feb 27, 2014 at 08:18 PM
Since there's no definitive answer on unityAnswers or the forums yet, I'll post Unity's response here:
It's hard-coded as far as I know. You can add different keys though as described in this code cample: http://docs.unity3d.com/Documentation/ScriptReference/GUI.GetNameOfFocusedControl.html
So KeyCode.Space is hard-coded to click the focused GUI.Button, but you can use Event.current to work around it.
Your answer
Follow this Question
Related Questions
changing from mouse click to keys 0 Answers
Is there something wrong with my c# script? 1 Answer
How to touch up the UI in real time? 1 Answer