I am attempting to make a game in C# in windows forms, and need for the character to react to key input. I have implemented a very basic code which changes the position of a picture box on key pressed, and then displays the picture box in a new position:
public Form1()
{
InitializeComponent();
KeyDown += new KeyEventHandler(keyDown);
}
void keyDown(object sender, KeyEventArgs e)
{
int x = SpriteCharacter.Location.X;
int y = SpriteCharacter.Location.Y;
if (e.KeyCode == Keys.Right) x += 1;
else if (e.KeyCode == Keys.Left) x -= 1;
else if (e.KeyCode == Keys.Up) y -= 1;
else if (e.KeyCode == Keys.Down) y += 1;
else if (e.KeyCode == Keys.Escape) Close();
SpriteCharacter.Location = new Point(x, y);
}
The problem you can probably immediately spot is that this script cannot handle user input if two keys are pressed at the same time. E.g. if you press right and down, the character will not move diagonally, but will either go right or down, depending on which key was pressed last.
What would be a more advanced way to handle key input, to make something like this possible? Is these anything I could read up on?