Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all
I want to use keyboard keys such as w,a,d,z instead of mouse to control for example a car in a game that created by c#.
thank you.
Posted

you can select the KeyDown event to apply that :)
try read more in MSDN help.
take care

this is an example:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // here we will ask if the w key is pressed or not
            if (e.KeyChar == 'w')

                //here the thing that will happen if we pressed w
                // for example:
                MessageBox.Show("w key is pressed");
        }
 
Share this answer
 
v3
You can use KeyDown or KeyPress. These events are reported on Control, but you may want to also handle PreviewKeyDown if you want to get events before they are 'hijacked' by control mnemonics or control navigation (particularly true for tab and arrow keys, but any letter can be used for an accelerator on a control somewhere).
 
Share this answer
 
In your Form, set Form1.KeyPreview[^] = true and handle KeyDown events.

Under KeyDown event you can Suppress KeyDown events, then they will never reach the active control.

C#
Form1.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
  ListBox1.Items.Add(e.KeyCode);
  e.Handled = false;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900