Rich
Rich

Reputation: 15

Is there a way to disable specific keys in C#?

I am trying to make it so that when my character jumps the jump key is disabled until his jumpHeight == 0 (he is back on the floor). Is there an easy function in C# to be able to do this? For example:

if (jumpHeight > 0)
{
   keys.space == disabled
}

Thanks in advance.

Upvotes: 0

Views: 224

Answers (1)

user932887
user932887

Reputation:

Your approach means more pointless work, and won't allow you to disable the actual input anyway. Instead, you should choose to not perform any checks on the status of particular keys if a condition is met.

public void Update(GameTime gt)
{
    if (canCheckSpace)
    {
         if (spaceIsPressed())
         {
              doThings();
         }
    }
    if (jumpHeight > 0)
    {
        canCheckSpace = false;
    }
}

If you wish to "disable" a key for all of the scripts checking for it, consider wrapping the input into your own manager, that will check for those conditions and act accordingly, for any script calling its public methods.

Upvotes: 5

Related Questions