- Home /
How to Use Key Combinations with the Control-Key?
In the Unity 5 editor, when I try to give input in Update with a control-key mod, the key-press of another key is not rightly registered.
If I have:
void Update()
{
if ((Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl)) &&
Input.GetKey(KeyCode.X)) Debug.Log(true);
}
And my input is: Left-Ctrl + X, nothing is printed. Same with Right-Ctrl + X.
However, if I have Shift + Ctrl + X, then "true" is printed! By not specifying "right" or "left," I mean to imply it does not matter which is the case.
And Alt + Ctrl + X also prints "true"
This is true for all the letters on a standard keyboard in-place of X, it would seem after testing a few.
However, if I have
void Update()
{
if ((Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl)) && Input.GetKeyDown(KeyCode.Alpha1))) Debug.Log(true);
}
By pressing a Ctrl-Key alone will solicit the output: "true"
I have no problem integrating the control-keys with mouse clicks nor shift or alt with other keys, so what is happening here that is messing with the input so?
Edit: I want to clarify that it works when I build the project, but as noted at the beginning this weird, and I hesitate to call it a bug, "bug" only exists in the editor. I'm trying to find a way around it.
I guess Ctrl key is caught by Unity editor which causes weird behaviors as you described. Try to use Ctrl + P key. It will start/stops the editor player. I confirm the code above. If you first press X then Ctrl, it works but if you first Ctrl then X, it never reads the X key. It is something with Editor I guess, it should work fine in builds.
Answer by TStudios13 · Dec 01, 2019 at 11:55 PM
Try this
if (Input.GetKey(KeyCode.RightControl) { if (Input.GetKey(KeyCode.LeftControl) { Debug.Log(true); }
Answer by ErikH2000 · Feb 14, 2021 at 07:51 PM
There are some answers over here - https://answers.unity.com/questions/49285/how-can-i-get-a-combination-of-keys-pressed.html?_ga=2.23479418.1148461192.1613246522-1228664066.1589857462