Disable player input when key pressed

Thanks in advance for your help with this. I’m a newbie and am sure there’s something simple I’m doing wrong.

Here’s what I’m trying to achieve:

Player pushes “2” on the keyboard which activates an object. If they push any other key, the object deactivates.

The following script flashes my object for a moment but I want it to be active until another key is pressed.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Button2 : MonoBehaviour
{
    [SerializeField] GameObject _object;

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            _object.SetActive(true);

        }

        if (!Input.GetKeyDown(KeyCode.Alpha2))
        {
            _object.SetActive(false);

        }
    }
} 

This code does not do what you intend. Input.GetKeyDown(KeyCode.Alpha2) will be true on the frame that the 2 key is pressed and will be false on all other frames. You probably want something like this:

if(Input.GetKeyDown(KeyCode.Alpha2))
{
    _object.SetActive(true);
}
else if(Input.anyKeyDown)
{
    _object.SetActive(false);
}