- Home /
Check the latest key pressed?
How can I check the latest key that the user pressed?
For example, when the user presses A, it will write "A" Then when the user presses B, it will only write "B", and will stop writing "A".
Here's an example of what I'm trying to say:
if(Input.GetKey(KeyCode.A))
{
Debug.Log ("A");
}
if(Input.GetKeyUp(KeyCode.B))
{
Debug.Log ("B");
}
//What I want to happen is:
//When the user presses A first, then B next, it will only write B, and not A.
//When the user presses B first, then A afterwards, it will only write A, and not B. It will just completely stop executing Debug.Log("B")
// BUT, with my current code, it will still execute both A and B even though I pressed A first, then B next, or vice versa. I only want one to be executed even though I'm pressing both keys, and the one the should be executed is the latest key I've pressed.
Write where? debug console? in game view? I'm guessing you know how to check for user input already.
Doesn't really matter, but yeah I guess in this case, the debug console.
$$anonymous$$eaning, it will stop writing A once it press B. It will only write the latest key that was pressed, even if the user is still pressing both A and B.
It's Debug.Log. What will be written depends on which button you are checking for in the update loop i guess. Debug will always show stuff when it's called, so you could have a private variable that is assigned to the buttons during your checks, and then fed to debug at the very end.
I know that. But how can I make it so that it will only write "B" if the B key is the latest key I've pressed, even though I'm still pressing A and B at the same time?
var ab = 4;
if keydown a, ab = 0;
if keyup a and key = 0, ab = 4;
if keydown b , ab = 1;
if keyup b and key = 1, ab = 4;
if ab = 0 print a if ab = 1 print b
Answer by kacyesp · Aug 30, 2014 at 05:32 PM
This works. In the case where you hold down A, then hold down B, but then let go of B, if you do NOT want A to be printed out, just remove the last else statement with the while loop. You can also remove the stack variable. I used a stack to give you a more flexible implementation in case you want to add more inputs.
using UnityEngine;
using System.Collections.Generic;
public class InputProcessor
{
private Keycode latestKey;
private Stack<KeyCode> stack = new Stack<KeyCode>();
void Update()
{
if( Input.GetKeyDown(KeyCode.A) )
latestKey = KeyCode.A;
else if( Input.GetKeyDown(KeyCode.B) )
latestKey = KeyCode.B;
if ( Input.GetKey(latestKey) )
Debug.Log( latestKey );
else
while (stack.Count != 0)
{
latestKey = stack.Pop();
if (Input.GetKey(latestKey))
{
Debug.Log( latestKey );
break;
}
}
} //end of function Update
} //end of class InputProcessor
Answer by frogsbo · Aug 30, 2014 at 05:11 PM
its technically known as radio buttons.
this should work:
var ab = 4;
if keydown a, ab = 0;
if keyup a and ab = 0, ab = 4;
if keydown b , ab = 1;
if keyup b and ab = 1, ab = 4;
if ab = 0 print a
if ab = 1 print b
you can also use a ab++ to count frames that it is pressed.
perhaps try more like this:
laskeydown int = 0;
if keydown a, lastkeydown = 1
if keydown b, laskeydown = 2
if keyup a and lastkeydown = 2, do nothing
if keyup a and lastkeydown = 1, lastkeydown = 0
if keyup b and lastkeydown = 1, do nothing
if keyup b and lastkeydown = 2, lastkeydown = 1
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Speed pickup 1 Answer
Controller menu keylist 1 Answer
My Players speed differs depending on the input method (gamepad or keyboard) 1 Answer
New Input System for Controller is not able to work with autogenerated C# class file Unity 2018.4 0 Answers