- Home /
Question by
jpc133 · Sep 11, 2014 at 08:40 PM ·
javascriptinputefficiency
Detecting and saving keyboard presses without using more than 20 if statements.
My question is if its possible to make more efficient code to handle keyboard press detection than what I already have. This is the code I use at the moment:
if(Input.GetKeyDown(KeyCode.Alpha1)){
screen_data[0] = screen_data[0] + "1";
}
if(Input.GetKeyDown(KeyCode.Alpha2)){
screen_data[0] = screen_data[0] + "2";
}
if(Input.GetKeyDown(KeyCode.Alpha3)){
screen_data[0] = screen_data[0] + "3";
}
if(Input.GetKeyDown(KeyCode.Alpha4)){
screen_data[0] = screen_data[0] + "4";
}
if(Input.GetKeyDown(KeyCode.Alpha5)){
screen_data[0] = screen_data[0] + "5";
}
if(Input.GetKeyDown(KeyCode.Alpha6)){
screen_data[0] = screen_data[0] + "6";
}
if(Input.GetKeyDown(KeyCode.Alpha7)){
screen_data[0] = screen_data[0] + "7";
}
if(Input.GetKeyDown(KeyCode.Alpha8)){
screen_data[0] = screen_data[0] + "8";
}
if(Input.GetKeyDown(KeyCode.Alpha9)){
screen_data[0] = screen_data[0] + "9";
}
if(Input.GetKeyDown(KeyCode.Alpha0)){
screen_data[0] = screen_data[0] + "0";
}
if(Input.GetKeyDown(KeyCode.Q)){
screen_data[0] = screen_data[0] + "q";
}
if(Input.GetKeyDown(KeyCode.W)){
screen_data[0] = screen_data[0] + "w";
}
if(Input.GetKeyDown(KeyCode.E)){
screen_data[0] = screen_data[0] + "e";
}
if(Input.GetKeyDown(KeyCode.R)){
screen_data[0] = screen_data[0] + "r";
}
if(Input.GetKeyDown(KeyCode.T)){
screen_data[0] = screen_data[0] + "t";
}
if(Input.GetKeyDown(KeyCode.Y)){
screen_data[0] = screen_data[0] + "y";
}
if(Input.GetKeyDown(KeyCode.U)){
screen_data[0] = screen_data[0] + "u";
}
if(Input.GetKeyDown(KeyCode.I)){
screen_data[0] = screen_data[0] + "i";
}
if(Input.GetKeyDown(KeyCode.O)){
screen_data[0] = screen_data[0] + "o";
}
if(Input.GetKeyDown(KeyCode.P)){
screen_data[0] = screen_data[0] + "p";
}
if(Input.GetKeyDown(KeyCode.A)){
screen_data[0] = screen_data[0] + "a";
}
if(Input.GetKeyDown(KeyCode.S)){
screen_data[0] = screen_data[0] + "s";
}
if(Input.GetKeyDown(KeyCode.D)){
screen_data[0] = screen_data[0] + "d";
}
if(Input.GetKeyDown(KeyCode.F)){
screen_data[0] = screen_data[0] + "f";
}
if(Input.GetKeyDown(KeyCode.G)){
screen_data[0] = screen_data[0] + "g";
}
if(Input.GetKeyDown(KeyCode.H)){
screen_data[0] = screen_data[0] + "h";
}
if(Input.GetKeyDown(KeyCode.J)){
screen_data[0] = screen_data[0] + "j";
}
if(Input.GetKeyDown(KeyCode.K)){
screen_data[0] = screen_data[0] + "k";
}
if(Input.GetKeyDown(KeyCode.L)){
screen_data[0] = screen_data[0] + "l";
}
if(Input.GetKeyDown(KeyCode.Z)){
screen_data[0] = screen_data[0] + "z";
}
if(Input.GetKeyDown(KeyCode.X)){
screen_data[0] = screen_data[0] + "x";
}
if(Input.GetKeyDown(KeyCode.C)){
screen_data[0] = screen_data[0] + "c";
}
if(Input.GetKeyDown(KeyCode.V)){
screen_data[0] = screen_data[0] + "v";
}
if(Input.GetKeyDown(KeyCode.B)){
screen_data[0] = screen_data[0] + "b";
}
if(Input.GetKeyDown(KeyCode.N)){
screen_data[0] = screen_data[0] + "n";
}
if(Input.GetKeyDown(KeyCode.M)){
screen_data[0] = screen_data[0] + "m";
}
if(Input.GetKeyDown(KeyCode.Period)){
screen_data[0] = screen_data[0] + ".";
}
if(Input.GetKeyDown(KeyCode.Backspace)){
if(screen_data[0] == ""){
}
else{
screen_data[0] = screen_data[0].Substring(0, screen_data[0].length - 1);
}
Comment
Don't have time for a long answer or example code, but take a look at Input.inputString. In addition you might take a look at GUI and the Event class. You can get both the keycode and the character just typed inside of OnGUI() using Event.current.
Your answer
Follow this Question
Related Questions
How to code input. 1 Answer
Play a simple animation once on key press 3 Answers
displaying playerprefs 1 Answer
Get touch position? 0 Answers
Text.Field erratically updating 0 Answers