Search code examples
c#winformsvisual-studio-2015keypress

Determine if key was pressed in Windows Forms


I want to make a game in C# Windows Forms Application. (Is it maybe a bad idea? :D)

I need to determine if key was pressed in my timer. The game is basicly objects like labels or pictureBoxes moving - their location is changed via timer and I need to determine when key is pressed.

I made the same game as a consoleApplication and I wanted to make a "graphic interface" for it, but obviously I can't use this part of my code:

if (Console.KeyAvailable)
    {
    ...
    }

I am pretty new to c# and programming itself, so I am truly sorry if my question is stupid :-)


Solution

  • Just handle the KeyPress event of the control which will have the focus.

    With Key press event, the event argument is of type KeyPressEventArgs which will give you details of which key is pressed