Swtich statement using keycode.X

I am at a lost on how to do a switch statement using a user input using the keycode.X what do I use to test the case? KeyCode or something like a variable or something like that?
Thanks

I don’t understand what you need.

Could you clarify?

I think you’re speaking on how to determine when a specific key is entered, outside of the use of the Input Manager. So instead of doing Input.GetButton(“name of button”), you want to know if the specific key was pressed using a predefined enumeration.

For example: Keys.F8 to designate the F8 button.
Or in XNA: keyboardstate.IsKeyDown(Keys.Space)

To my knowledge, Unity doesn’t have this, but I am no way a Unity master.

What is it you are planning on doing with the switch statement?

unity has Input.GetKeyDown(Kexcode.X) - if that is what you are looking for

Good to know. :slight_smile: To extend this:

Input:

Keycode:

What i want to do is get rid of all my if statements and use a nice and tidy switch statement:
This is just a snipit of the code that I am using and its geting out of hand the more I have to put into it.

if (Input.GetKeyDown(KeyCode.H)){
	FlameSlash();
}//flameSlash

//shiningStrike
if (Input.GetKeyDown(KeyCode.J)){
		ShiningStar();
}//end shiningStrike

//slash
if (Input.GetKeyDown(KeyCode.K)){
	Slash();
}//end of slash

But I dont know how to use the switch statment instead of the if: What do I use to test the case

switch(Dont know what to put here to test what button to push)
{
  I know what to put in here

}
1 Like

There isn’t an Input class function to get which key is being pressed and there isn’t a way to make the boolean Input functions work usefully with a switch statment. You can use the Event.keyCode variable for this, but it is only valid in the OnGUI function during KeyDown and KeyUp events. You may be able to get the key code from the OnGUI function and store it in a variable where it can by picked up by Update later on. Generally speaking, more than one key could be held down at once, so you should use a switch statement cautiously, in any case.

Thanks for the input everyone, I think I will stick with the if statements that I have right now.

if (Input.anyKeyDown)
{
switch(Input.inputString);
}

2 Likes

Input.inputstring will give you regular alphabet letters but everything else is “”. ie. leftarrow = “”, m = “m”