I would do it as follows:
void Update()
{
if (Input.anyKeyDown)
{
if (Input.GetKeyDown(KeyCode.Up)
Debug.Log("Up Arrow key was pressed");
else if (Input.GetKeyDown(KeyCode.Right)
Debug.Log("Right Arrow key was pressed");
else if (Input.GetKeyDown(KeyCode.Down)
Debug.Log("Down Arrow key was pressed");
else if (Input.GetKeyDown(KeyCode.Left)
Debug.Log("Left Arrow key was pressed");
}
}
maybe you would want a bunch of ifs instead of "else if"
or you could explore a way to make this work:
void OnGUI()
{
var input = Event.current;
switch (input.keyCode)
{
case KeyCode.Up:
Debug.Log("Up Arrow key was pressed");
break;
case KeyCode.Right:
Debug.Log("Right Arrow key was pressed");
break;
case KeyCode.Down:
Debug.Log("Down Arrow key was pressed");
break;
case KeyCode.Left:
Debug.Log("Left Arrow key was pressed");
break;
}
but in my experience it glitches, there is certainly a way to do it correctly but this code in my project its not stable as it runs multiple times while you press the key once.