- Home /
The question is answered, right answer was accepted
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);
}
Answer by numberkruncher · Jun 01, 2013 at 11:46 PM
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 :)
void OnGUI() {
//for (int i = 0; a < 9; i++){
if (Event.current.type == EventType.$$anonymous$$eyDown) {
if (Event.current.keyCode >= $$anonymous$$eyCode.Alpha1
&& Event.current.keyCode <= $$anonymous$$eyCode.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();
}
}
}
}
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.$$anonymous$$eyDown) {
if (Event.current.keyCode >= $$anonymous$$eyCode.Alpha1
&& Event.current.keyCode <= $$anonymous$$eyCode.Alpha9) {
someint = i;
Event.current.Use();
}
}
}
}
}
Follow this Question
Related Questions
C# Number Key For Loop 1 Answer
C# Non-Static Member Rigidbody2D.MovePosition 1 Answer
How to make specific text in a string array Bold C# 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Movement Script Help 1 Answer