Q:

Flipping a boolean value

I have a small C# problem that I'm sure must be easy to solve, but I can't for the life of me figure out what's wrong.
I have a debug mode in my game which is turned on when a boolean value debugMode is set to true. The variable is held within a gameInfo object, referenced in my GameInfo class. I want to make it so that I can toggle debug mode on and off by pressing the "I" key on the keyboard. Here's what I tried:
if (keyboardState.IsKeyDown(Keys.I))
      GameInfo.gameInfo.debugMode = !GameInfo.gameInfo.debugMode;

The problem is that nothing happens when I press "I". If I do the following I am able to turn debug mode off so I know the idea works:
if (keyboardState.IsKeyDown(Keys.I))
          GameInfo.gameInfo.debugMode = false;

Does anyone know what I'm doing wrong? I can of course solve it by using a couple of if statements to turn a true to a false and vice versa, but that it long-winded compared with the simple statement above.
Thanks a lot for any help you can offer :)

A:

This does require you to have another flag, but it will prevent the Key event from being handled multiple times at once.
bool keyPressRegistered = false;

...

if (keyboardState.IsKeyDown(Keys.I) && !keyPressRegistered)
{
    keyPressRegistered = true;
    GameInfo.gameInfo.debugMode = !GameInfo.gameInfo.debugMode;
}

...

keyPressRegistered = !(keyPressRegistered && keyboardState.IsKeyUp(Keys.I));

