How To Detect Any Key Press But Ignore Mouse – Unity C#

There might be a time you want to check if the player has pressed any key on the keyboard – but you do not want Unity to check for mouse clicks. This can be frustrating, because for some reason, the line of code you would expect to work checks for both keyboard presses AND mouse clicks:

if (Input.anyKeyDown)
{ ... }

I don’t know why Unity didn’t separate out mouse clicks and keyboard presses into two separate lines of code, but there it is.

How To Do It

Fortunately, the workaround is simple:

    private void Update()
    {
        if (Input.anyKeyDown && !(Input.GetMouseButtonDown(0) 
            || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2)))
        {
            Debug.Log("No mouse pressed!");
        }
    }

The solution is a slightly more complicated if statement. First we check if any key is down, including mouse clicks, but then we also make sure that none of the mouse buttons are being pressed.

If you are confused by what &&, || and ! mean in the if statement, read my article here explaining them.

How Not To Do It

I have seen people suggest an alternative method online, but that is a very bad idea. The alternative is:

    private void Update()
    {
        if (Input.anyKeyDown)
        {
            if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)
                || Input.GetMouseButtonDown(2))
            {
                return; // Do nothing
            }               
        }
    }

There is a serious problem with this solution, though, which you’ll see if you test the following code:

    private void Update()
    {
        if (Input.anyKeyDown)
        {
            if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)
                || Input.GetMouseButtonDown(2))
            {
                return; // Do nothing
            }               

            // Do something 
            Debug.Log("No mouse pressed!");
        }

        if (Input.anyKeyDown)
        {
            Debug.Log("Code that should run!");
        }
    }

If you test that code, and click the mouse, you will notice that the message, “Code that should run!” will never print when you click the mouse, even though it technically should, because we are checking if any key is down including mouse presses. But actually it shouldn’t run, because earlier, we said that if a mouse button is pressed, to Return and do nothing.

The word Return will exit out of the entire function, so if you used that in your game, any other code you had in your update function, like code for controlling AI, would not run that frame, and this could cause some really weird and hard to detect bugs. So don’t do it that way. You could sort of get around it if you put that code at the bottom of the Update function, but that’s still a bad idea when there’s really a better way.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s