- Home /
C# Number Key For Loop
Hi everyone, I'm trying to make my number keys work with a for loop. But it doesn't appear to be working. Any idea what's wrong with it?
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();
}
}
}
}
Answer by Bunny83 · Jun 02, 2013 at 03:57 AM
I guess you just want to know which key has been pressed in your someInt variable?
Just do it like this:
public int someInt;
void OnGUI()
{
Event e = Event.current;
if (e.type == EventType.KeyDown)
{
int key = (int)e.keyCode - (int)KeyCode.Alpha1;
if (key >= 0 && key < 9)
{
someInt = key;
Event.current.Use();
}
}
}
someInt will be set to a number of 0 - 8 (matching key 1 - 9) when one of the Alpha keys is pressed.
No, I want to get it to work with for loops. I have other things that I need to work with like strings and booleans. I would like to get it to work like the way I have my GUI.Buttons setup.
public string[] someText;
public string someString;
void OnGUI () {
someText= new string[]
{"Text1","Text2","Text3","Text4","Text5","Text6"};
for(int i = 0; i < someText.Length; i++){
GUILayout.Button(someText[i], GUILayout.Width(142), GUILayout.Height(25)){
someString = someText[i];
}
}
If you have different codes for different scenario, just use a switch-case. If you need to work with arrays, just use the someInt
as the array index.
void OnGUI()
{
Event e = Event.current;
if (e.type == EventType.$$anonymous$$eyDown)
{
...
switch( someInt ) {
case 0:
//Do stuff
break;
case 1:
//Do stuff
break;
}
}
}
If you really loves loop, then:
private $$anonymous$$eyCode[] key;
void Start() {
key = new $$anonymous$$eyCode[] {
$$anonymous$$eyCode.Alpha1,
$$anonymous$$eyCode.Alpha2,
...
};
}
void OnGUI()
{
Event e = Event.current;
if (e.type == EventType.$$anonymous$$eyDown)
{
for( int i = 0; i < key.length; ++i ) {
if( e.keyCode == key[i] ) {
//Do stuff here
}
}
}
}
A for loop makes no sense at all in this situation. Unity will call OnGUI for specific events. In the case of GUI.Buttons it's fine to use a for loop since each Button has to check it's screen area when a $$anonymous$$ouseDown / $$anonymous$$ouseUp event occurs. However key-events aren't context-related. So through what colletion do you want to iterate with your for loop? The only reasonable thing would be to use Chronos-L second example, but it would do exactly the same as my solution. $$anonymous$$y key variable would match your "i" variable.
Here's a combined example:
public string[] someText = new string[]{"Text1","Text2","Text3","Text4","Text5","Text6"};
public string someString;
void OnButtonAction(int aIndex)
{
someString = someText[aIndex];
}
void OnGUI ()
{
Event e = Event.current;
for(int i = 0; i < someText.Length; i++)
{
if (GUILayout.Button(someText[i], GUILayout.Width(142), GUILayout.Height(25))
{
OnButtonAction(i);
}
}
if (e.type == EventType.$$anonymous$$eyDown)
{
int key = (int)e.keyCode - (int)$$anonymous$$eyCode.Alpha1;
if (key >= 0 && key < someText.Length)
{
OnButtonAction(key);
Event.current.Use();
}
}
}
This would display 6 buttons and the user can either click one of the buttons or press the corresponding key to execute the action.
Your answer
Follow this Question
Related Questions
C# Accessing keys 1-9 All at Once 1 Answer
How to Name Individual Buttons Within a List 2 Answers
c# Adjust In-Game audio 1 Answer
C# How to Detect Edges of a Collider 0 Answers
FPS keep a loadout 0 Answers