I'm wondering if there is a way to detect if the key being pressed is NOT matching a specific KeyCode.

I tried using: !(Input.GetKeyDown(exampleKeyCode)), but that also detects if the key is being released.

I also tried using
(!(Input.GetKeyDown(exampleKeyCode)) && !(Input.GetKeyUp(exampleKeyCode))
but that doesn't work because when a key is not down it is up, and when it is not up it is down.


1 Answer 1


You can first check whether "any" key has been pressed, then check if the key you're listening to has not, to respond only to presses that were not that key.

if (Input.anyKeyDown) {
    if (Input.GetKeyDown(exampleKeyCode)) {
        Debug.Log("The example key (and maybe others) was pressed this frame");
    } else {
        Debug.Log("One or more keys were pressed this frame, but not the example key.");

Note however that anyKeyDown does not reset until all keys have been released. So if I hold A it will return true once. If I then press B while still holding A, it will continue reading false. Only once I release all keys then press a key in that "no keys held" state will it fire true again.

If you want to more robustly identify all key presses that were not "this" key, even while a key is held, you can switch to Unity's new Input System package, which is more powerful. There's a thread here discussing ways to implement this kind of functionality with that package.

  • \$\begingroup\$ I can't believe I didn't think of that! \$\endgroup\$ Nov 23, 2023 at 21:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .