Unity keycode 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 (Numeric input)

unity3d numeric input keycodes 1 2 3

In this article I’ll cover a very simple topic – defining numeric keys pressed by user in Unity3D (like 0, 1, 2, 3, etc).

In Unity user input is recognized by Input API that contains methods like GetAxis, GetKeyDown, GetButtonDown and others. In this example we will use GetKeyDown method that takes a single argument – KeyCode. KeyCode is an integer number that symbolizes a concrete keyboard key.

Unity Numeric KeyCodes

To determine numeric key input in Unity we need to use specific KeyCode constants that starts with word “Alpha”:

  • KeyCode.Alpha0 – 0 key
  • KeyCode.Alpha1 – 1 key
  • KeyCode.Alpha2 – 2 key
  • KeyCode.Alpha3 – 3 key
  • KeyCode.Alpha4 – 4 key
  • KeyCode.Alpha5 – 5 key
  • KeyCode.Alpha6 – 6 key
  • KeyCode.Alpha7 – 7 key
  • KeyCode.Alpha8 – 8 key
  • KeyCode.Alpha9 – 9 key

Unity KeyCode 1 example

Let’s see an example code, where we will check if user pressed key “1”.

Such functionality should be places inside of Update or FixedUpdate methods of the object that will handle user’s input.

void Update() {
	if (Input.GetKeyDown (KeyCode.Alpha1)) {
		Debug.Log ("User pressed key 1");
        }
}

This code should write to your log “User pressed key 1” each time you press 1 in editor’s game mode.

In next articles I will try to cover the whole concept of player controls using code examples from a Unity3d project.

Leave a Reply

Be the First to Comment!