0

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 :-)

Megabight
  • 71
  • 1
  • 1
  • 10
  • http://csharp.net-informations.com/gui/key-press-cs.htm – Steven Ackley Nov 18 '15 at 17:47
  • Possible duplicate of [Key press events in C# -- Moving a PictureBox](http://stackoverflow.com/questions/5956207/key-press-events-in-c-sharp-moving-a-picturebox) – Jonathan Carroll Nov 18 '15 at 17:48
  • Possible duplicate of [Best way to implement keyboard shortcuts in a Windows Forms application?](http://stackoverflow.com/questions/400113/best-way-to-implement-keyboard-shortcuts-in-a-windows-forms-application) – MicroVirus Nov 18 '15 at 18:31

2 Answers2

0

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

Kapoor
  • 1,388
  • 11
  • 21
0

You should add this in your Form1 class:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.A) MethodForProcessingTheKey();//for example
    return base.ProcessCmdKey(ref msg, keyData);
}
User42
  • 370
  • 1
  • 5