Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when two keys are pressed at the same time

Tags:

c#

.net

key

I have no idea how do this.

I know only how do detect one key:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.C)
    {
        MessageBox.Show("C key");
    }
}
like image 853
The Mask Avatar asked Dec 16 '11 15:12

The Mask


2 Answers

You have to keep track of keydown/keyup events, and keep a list of all the keys that are currently "down". The keyboard handler can only trigger on individual keys, and it's up to your code to detect/keep track of which ones are down, and if those individual keydown events are close enough to each other to be counted as "together".

like image 110
Marc B Avatar answered Nov 11 '22 01:11

Marc B


put a break point in your key down event and press your two keys together.
examine the KeyData of the KeyEventArgs. it will show you what you have to use to detect two keys pressed together. Use some dummy code like this:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    MessageBox.Show("KeyData is: " + e.KeyData.Tostring());
}

like I have done for shift and r pressed together

e.KeyData = R | Shift

like image 4
Ahmed Shahid Avatar answered Nov 11 '22 00:11

Ahmed Shahid