- Home /
Multiple Input.GetKey
Hi,
I want a piece of code which says "if none of the following keys of pressed then..."
So this is what I think is most suitable. (I prefer Javascript).
if (!(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))) {
// Then do what I want.
}
(I haven't tried it yet but) it's extremely long, and the length is quite annoying. Is there another way to do this, something like this:
if (!(Input.GetKey(KeyCode.UpArrow, Keycode.W //etc...
So how I could do this?
PS: This is different to if any key is pressed. Thanks!
hey sry for necromancy but i have some issues to get the input right
ich check if left or right is pressed and then let the player move if up is press he jumps if i press lef or right the jump works fine to but somehow not allway i have a seceond function wich le the charakter sprint when i hit left or right and space
no there is a problem if i push right space and up arrow he jumps out of sprinting but not if i hit left space and up arrow :(
on a diffrent cumputer this works but somehow other input stuff is buggy i guess it has something to do with the keyboardbuffer wich is has a overflow or something in this case any idea how to handle this? would be great
Answer by Owen-Reynolds · Jul 05, 2013 at 06:48 PM
A few weeks into a regular bring programming class you'd see two ways to handle "none of the above":
if(Input.GeyKeyDown("a")) { .... }
else if(Input.GetKeyDown("b")) { ..... }
else if ...
else // None of the above goes here
If you can't organize things that neatly, the flag trick also works:
bool keyWasHandled=false;
if(Input.getKetDown("y")) { ... keyWasHandled=true; }
// everyone who processes a key should set: keyWasHandled=true
if(Input.AnyKeyDown && keyWasHandled==false) {
// we have a key that no one else wanted
}
The array method (Bunny, above) is "better," but only if you know arrays and, everyone else who's going to mess with your code.
Your first solution won't probably not work for his case ;) It looks like he has the classical WASD and the arrow-keys movement input. So when doing elseif chains only one of the ifs can be executed. The second one will of course work and is in most cases the best solution. It avoids redundancy of keynames. I just answered the question most straight-forward ;)
Actually UT could change the Input.Get$$anonymous$$ey function so it would take a params array since that wouldn't break any existing code
Well all I want the of statements to do is change a static variable, so this works for me.
Answer by Bunny83 · Jul 05, 2013 at 01:15 PM
Well, in C# you can create a function with a params parameter. This allows you to pass as much parameters as you like. I'm not sure if that's possible in UnityScript.
This would work in C#
bool GetAnyKey(params KeyCode[] aKeys)
{
foreach(var key in aKeys)
if (Input.GetKey(key))
return true;
return false;
}
This function could be used like this:
if (GetAnyKey(KeyCode.UpArrow, Keycode.W /* , ... */ ))
Or for your case
if (!GetAnyKey(KeyCode.UpArrow, Keycode.W /* , ... */ ))
As general alternative you could define your keys in an array and pass the array to the function.
Answer by mentaldepth · May 04, 2015 at 11:50 AM
Here it is working :D thanks for info guys. (very simple but it works all directions no sound cut off)
var AudioFile : AudioClip;
function Update() {
if ( Input.GetButtonDown( "Horizontal" ) || Input.GetButtonDown( "Vertical" ) )
{
GetComponent.<AudioSource>().clip = AudioFile;
GetComponent.<AudioSource>().Play();
}
else if ( !Input.GetButton( "Horizontal" ) && !Input.GetButton( "Vertical" ) && GetComponent.<AudioSource>().isPlaying )
{
GetComponent.<AudioSource>().clip = AudioFile;
GetComponent.<AudioSource>().Stop();
}
}
Answer by JohnnySunshine · Jul 05, 2013 at 01:02 PM
You could use input axes; by default, you have "Vertical" and "Horizontal" mapped to arrow keys and WASD, which gives you:
if (Mathf.Approximately(0.0f, Input.GetAxis("Horizontal") && Mathf.Approximately(0.0f, Input.GetAxis("Vertical"))
{
// Then you do what you want
}
Answer by calegxm999 · Jun 23, 2020 at 06:41 AM
if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.RightArrow))
Your answer
Follow this Question
Related Questions
Controlling animation speed 2 Answers
Override windows onaly allowing three buttons being pressed 1 Answer
Very wierd issue with right arrow key 3 Answers
gui.button to enable keypress 2 Answers
Returning Which Key Was Pressed 1 Answer