x


[Closed] 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[i])){
    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);
            }
more ▼

asked Jun 01, 2013 at 11:21 PM

DangerousBeans gravatar image

DangerousBeans
475 282 726 293

(comments are locked)
10|3000 characters needed characters left

The question has been closed Jun 02, 2013 at 03:30 AM by DangerousBeans for the following reason:

The question is answered, right answer was accepted


1 answer: sort voted first

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

Here is a simple example - Warning, not tested ;)

// 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 :)

more ▼

answered Jun 01, 2013 at 11:46 PM

numberkruncher gravatar image

numberkruncher
4.3k 139 97 122

    void OnGUI() {
     //for (int i = 0; a < 9; i++){
    if (Event.current.type == EventType.KeyDown) {
    if (Event.current.keyCode >= KeyCode.Alpha1
    && Event.current.keyCode <= KeyCode.Alpha9) {
    // Create three GUI.Boxes when one of the keys is pressed
    // and have them each be different depending on which key was pressed
    //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);
    Event.current.Use();
      }
    }
  }
}
Jun 01, 2013 at 11:55 PM DangerousBeans

I've updated my answer for you :)

Jun 02, 2013 at 12:35 AM numberkruncher

Can I possible use a for loop with this? I really want to use a for loop to make it a bit easier to work with.

public int someint;
    void OnGUI () {
for (int i = 0; i < 9; i++){
 if (Event.current.type == EventType.KeyDown) {
if (Event.current.keyCode >= KeyCode.Alpha1
&& Event.current.keyCode <= KeyCode.Alpha9) {
someint = i;
Event.current.Use();
                }
            }
        }
    }
}
Jun 02, 2013 at 12:49 AM DangerousBeans
(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x8911
x8524
x6096
x110
x68

asked: Jun 01, 2013 at 11:21 PM

Seen: 979 times

Last Updated: Jun 02, 2013 at 01:40 AM