Introduction
Recently I have bumped to a simple but for me as C# beginner unsolvable problem.
How to use WASD keyboard keys in a manner as in any other computer game ?
Solution for such problem involves handling keyboard event of simultaneously pressed two or more keys.
In my program I have used two standard keyboard Key event handlers.
OnKeyDown
and OnKeyUp
for determining what keys are pressed and released,
but the program did not worked correctly.
When I press key A and while holding it down I press key W,
program works perfectly but when I release key W
the OnKeyDown
event handler do not recognize that key A is still pressed.
After a lot of search for knowledge about such problem throughout the Internet,
I have found this answer :
Quote:
–
And finally here is an example of program that solves problem good enough
to be publicly shared as an TIP & Trick on Code Project.
Program overview
Program is developed as windows form application in C# 4.0 .Net 2.0.
After program start, by pressing WASD keys on the keyboard,
user can observe trough key flag counter value shown on screen,
how many times program has checked and confirmed the key down state of WASD keys.
Download open source code - 39.6 KB
Tip & Trick
Since that the standard OnKeyDown
and OnKeyUp
event handlers
can register when user press and release only one key and
not simultaneously pressed and released two or more keys,
the exception is key combination with Alt, Shift and Control key,
in this program those event handlers with key down flag variables
for each observed key, are used for determining and memorizing key down/up state.
For raising an action upon key down/up flag state program uses System.Windows.Forms.Timer
class object that raises event at user defined time interval. It serves inside program as infinite loop with constant time
interval set in milliseconds, for raising Timer.Tick
event handler trough which program calls user Main_Function(), where are defined actions that program is going to make upon user press/release keyboard key.
Using the code
Program code attached with this tip is designed for learning.
It is well commented and comments cover every aspect of problem solving.
Read it carefully and thoroughly.
For practical use, it is recommended to download the full solution created in IDE Sharp Develop. Open project in the same IDE or in the Visual Studio, build it and run program.
Download open source code - 39.6 KB
using System;
using System.Windows.Forms;
namespace WASD_keyboard_game_control
{
public partial class MainForm : Form
{
Timer Clock;
const int Interval = 15;
int W = 0;
int A = 0;
int S = 0;
int D = 0;
bool w = false;
bool a = false;
bool s = false;
bool d = false;
public MainForm()
{
InitializeComponent();
Initialize_Timer();
}
void Initialize_Timer()
{
Clock = new Timer();
Clock.Interval = Interval;
Clock.Tick += new System.EventHandler(this.ClockTick);
Clock.Start();
}
void ClockTick(object sender, EventArgs e)
{
Main_Function();
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if(e.KeyCode == Keys.W)
w = true;
if(e.KeyCode == Keys.A)
a = true;
if(e.KeyCode == Keys.S)
s = true;
if(e.KeyCode == Keys.D)
d = true;
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
if(e.KeyCode == Keys.W)
w = false;
if(e.KeyCode == Keys.A)
a = false;
if(e.KeyCode == Keys.S)
s = false;
if(e.KeyCode == Keys.D)
d = false;
}
void Main_Function()
{
if(w)
W++;
if(a)
A++;
if(s)
S++;
if(d)
D++;
Label_W.Text = "W = " + W.ToString();
Label_A.Text = "A = " + A.ToString();
Label_S.Text = "S = " + S.ToString();
Label_D.Text = "D = " + D.ToString();
}
}
}
Points of Interest
Learn how to handle simultaneously pressed two or more keyboard keys event and
simulate WASD game controls inside windows form application by using Timer class object.
After learning how to solve above problem,
I have learned that System.Windows.Forms.Timer
class with its Timer.Tick
event
is interesting and useful for many other similar applications.
History
Last updated 22.10.2016, Author