- Home /
Input.GetKey not working with W and S
string s = "";
if (Input.GetKey (KeyCode.W)) {
s += "W";
} if (Input.GetKey (KeyCode.S)) {
s += "S";
} if (Input.GetKey (KeyCode.A)) {
s += "A";
} if (Input.GetKey (KeyCode.D)) {
s += "D";
}
Debug.Log (s);
I wrote the code above to attempt to debug a problem I've been having. This code works expectedly unless both W and S are pressed in which case it stays as it was before. If W and D are pressed and S is added, the log says "WD". If W and S are pressed it will say "WS" but if A or D is added it will stay as "WS" and A or D will not be added. When A and D are pressed it works fine, and you can add W or S to the string.
If you change the code to
string s = "";
if (Input.GetKey (KeyCode.A)) {
s += "A";
} if (Input.GetKey (KeyCode.D)) {
s += "D";
} if (Input.GetKey (KeyCode.W)) {
s += "W";
} if (Input.GetKey (KeyCode.S)) {
s += "S";
}
Debug.Log (s);
the same problem occurs with W and S only. How would I fix this?
Could be a problem with key ghosting. Because of the way keyboards work, some key combinations can not be detected.
https://www.microsoft.com/appliedsciences/antighostingexplained.mspx
That's interesting! I thought that most keyboards used a form of multiplexing, but this explains why some keyboards are worse than others at detecting multi-key input.
For a reference I can add that if I hold W & S I can add A but not D so it is most likely a keyboard limitation and not a code one. If I hold W & D then both S and A is not read.
Your answer
![](https://koobas.hobune.stream/wayback/20220612112121im_/https://answers.unity.com/themes/thub/images/avi.jpg)