KeyDown : recognizing multiple keys

Kai picture Kai · Aug 12, 2009 · Viewed 79.5k times · Source

How can I determine in KeyDown that CtrlUp was pressed.

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Control && e.KeyCode == Keys.Up)
    {
        //do stuff
    }
}    

can't work, because never both keys are pressed exactly in the same second. You always to at first the Ctrl and then the other one...

Answer

Garry Shutler picture Garry Shutler · Aug 12, 2009

You can check the modifiers of the KeyEventArgs like so:

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up && e.Modifiers == Keys.Control)
    {
        //do stuff
    }
}  

MSDN reference