C# Accessing keys 1-9 All at Once

Hi everyone, I’m trying to make a script that checks to see if any of the keys 1-9 have been pressed. But I’m getting an error from the console saying keycode doesn’t contain a definition for alpha. Is there a way to access keys 1-9 all at once like this?

	void OnGUI () {
	  for (int i = 0; a < 9; i++){
	 if(Input.GetKeyDown(KeyCode.Alpha*)){*
  • GUI.Box (new Rect (0,Screen.height - 50,100,50), “Item”+ i.ToString());*
  • GUI.Box (new Rect (Screen.width/2,Screen.height/2,100,50), “Item”+ i.ToString()+1 );*
  • GUI.Box (new Rect (Screen.width - 100,Screen.height - 50,100,50), "Item "+ i.ToString()+2);*
  •  	}*
    

Yes this can be achieved easily using the Event object instead.

Here is a simple example - Warning, not tested :wink:

// Zero indicates that no number key is pressed.
private int numDown;

void OnGUI() {
    // Render boxes if numeric key 1-9 is pressed.
    if (numDown != 0) {
        GUI.Box(
            new Rect(0, Screen.height - 50, 100, 50),
            "Item " + numDown
        );
        GUI.Box(
            new Rect(Screen.width/2, Screen.height/2, 100, 50),
            "Item " + (numDown + 1)
        );
        GUI.Box(
            new Rect(Screen.width - 100, Screen.height - 50, 100, 50),
            "Item " + (numDown + 2)
        );
    }

    // Process keyboard events as needed.
    int num;
    if (Event.current.type == EventType.KeyDown) {
        // Convert to numeric value for convenience :)
        num = Event.current.keyCode - KeyCode.Alpha1 + 1;

        // Avoid having multiple number keys pressed!
        if (numDown == 0 && num >= 1 && num <= 9) {
            numDown = num;
            Event.current.Use();
        }
    }
    else if (Event.current.type == EventType.KeyUp) {
        num = Event.current.keyCode - KeyCode.Alpha1 + 1;
        if (numDown == num) {
            numDown = 0;
            Event.current.Use();
        }
    }
}

I am unclear as to why you are attempting to place GUI.Box for the split second in which the key is pressed. Perhaps you could comment with greater detail and I will amend my answer for you :slight_smile: