Search code examples
c#keypress

defaulting key-press variable in C#


I'm trying to write a game in C# and am receiving keyboard controls from the user. (ex: arrow keys)

I use a loop to look for key presses during game-play and I'd like for the value the key-press stores in my variable to not act like the key is being pressed constantly. How can I reset the variable I store the key-press in?

for (int t = 0; t < 10 || keyStrike.Key == ConsoleKey.Escape; t++) //t is the time of each "up line" : (10 in this case)
{
      if (Console.KeyAvailable == true)
              keyStrike = Console.ReadKey(true);

      int z = x; //saves original position

      //slides user right or left
      x += ((keyStrike.Key == ConsoleKey.RightArrow) ? 1 : 0) - ((keyStrike.Key == ConsoleKey.LeftArrow) ? 1 : 0);
      WriteAt("█", x, 34);
      WriteAt(" ", z, 34);

      //keyStrike reset here
}

Solution

  • If you want to reset the value just use default()

    keyStrike = default(ConsoleKeyInfo);