- Home /
How to return key pressed from Update?
As the question asks is there a way to return the KeyCode of a button pressed from the UPDATE function? I am aware you can achieve this from the OnGUI method using the Event handler but I am already using the OnGUI method for something else and considering I am changing gamestate stuff I would rather do it from the Update method.
My current reasoning is I want to user to be able to select any of the 10 number buttons while holding down control. I could do a branching if/else statement but I was hoping I could check if the key pressed returned a KeyCode that was contained inside my KeyCode List and thus branch off appropriately (sort of like making a pseudo switch) or is the only way to do this to hard code the values inside the if / else statement?
I'd prefer answers in Pseudo code or c# if possible I find Javascript a bit harder to read.....
Answer by Vonni · Apr 11, 2013 at 05:10 AM
Something like this?
var keyList : KeyCode[];
if(Input.GetKey(KeyCode.Ctrl)){ // or however ctrl is checked
for(var key : KeyCode in keyList){
if(Input.GetKeyDown(key)){
// weeeeeeeeeeeeeeeeee
}
}
}
Thanks didn't realise I could do that, for those interested in the c# implementation I used the following to test:
if(Input.GetAxis("controlGroup") != 0) {
foreach ($$anonymous$$eyCode key in codes) {
if(Input.Get$$anonymous$$eyDown(key)) {
print(codes.IndexOf(key));
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613100105im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a key stored, without using OnGUI 1 Answer
How to Use Key Combinations with the Control-Key? 2 Answers
Regaining system resources? 1 Answer
Question on Update() 1 Answer
Update increment error (2 + 1 = 0?) 1 Answer