- Home /
How to detect which key was pressed?
In the new versions of unity I've found that code such as:
Event keyEvent;
keyEvent = Event.current;
key = keyEvent.keyCode;
No longer works, I am trying to find a way to do this now that does, hopefully, something that can be run in the update method. Anything helps :)
Comment
Best Answer
Answer by smark12007 · Jul 25, 2020 at 07:43 AM
foreach (int val in Enum.GetValues(typeof(KeyCode))) {
KeyCode keyCode = (KeyCode)val;
if (Input.GetKey(keyCode)) { // or GetKeyDown
key = keyCode; // catch the input
// break the loop?
}
}
Edit: Variable keyCode will catch the input from user.
So in the catch the input section, what would I be setting key to? I'm trying to use this to set a keybinding when the user clicks a button.
Whatever you need! $$anonymous$$aybe like key = (KeyCode)val;
Your answer
