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.

1 Answer

1

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: Unity - Scripting API: GUI.GetNameOfFocusedControl

So KeyCode.Space is hard-coded to click the focused GUI.Button, but you can use Event.current to work around it.