KeyDown : recognizing multiple keys

Viewed 84832

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...

13 Answers

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

In the KeyEventArgs there are properties Ctrl, Alt and Shift that shows if these buttons are pressed.

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Up && Keyboard.IsKeyDown(Key.LeftCtrl))
    {
         //do stuff
    }
}

This code will work only if you press first LeftCtrl, then "UP". If order has no importance, I recommend that one :

if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))&& Keyboard.IsKeyDown(Key.Z))
{
    //do stuff
}

In that case, both Ctrl are taken in account, and no importance about the order.

You can use the ModifierKeys property:

if (e.KeyCode == Keys.Up && (ModifierKeys & Keys.Control) == Keys.Control)
{
    // CTRL + UP was pressed
}

Note that the ModifierKeys value can be a combination of values, so if you want to detect that CTRL was pressed regardless of the state of the SHIFT or ALT keys, you will need to perform a bitwise comparison as in my sample above. If you want to ensure that no other modifiers were pressed, you should instead check for equality:

if (e.KeyCode == Keys.Up && ModifierKeys == Keys.Control)
{
    // CTRL + UP was pressed
}

I tested below code. it works...

private void listView1_KeyDown(object sender, KeyEventArgs e)
    {
        if ((int) e.KeyData == (int) Keys.Control + (int) Keys.Up)
        {
            MessageBox.Show("Ctrl + Up pressed...");
        }
    }

It took me a while to find the hint I ultimately needed when trying to detect [Alt][Right]. I found it here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/4355ab9a-9214-4fe1-87ea-b32dfc22946c/issue-with-alt-key-and-key-down-event?forum=wpf

It boils down to something like this in a Shortcut helper class I use:

public Shortcut(KeyEventArgs e) : this(e.Key == System.Windows.Input.Key.System ? e.SystemKey : e.Key, Keyboard.Modifiers, false) { }

public Shortcut(Key key, ModifierKeys modifiers, bool createDisplayString)
{
    ...
} 

After "re-mapping" the original values (notice the e.Key == System.Windows.Input.Key.System ? e.SystemKey : e.Key part), further processing can go on as usual.

if (e.Control && e.Shift && e.KeyCode == Keys.A) {

}

The KeyDown event will only hold the information for the most recent key that was pressed. I have had success building a string that contains all the most recent key down keys concatenated together.

If a key down of "Control" was clicked, or the strings becomes greater than 10 chars long, I clear the string.

Checking the string each time, then performing a task afterwords gives a great use for a secret hotkey function within your application.

    private void ConfigurationManager_KeyDown(object sender, KeyEventArgs e)
    {
        string currentKey = e.KeyCode.ToString().ToLower();

        if ((currentKey == "controlkey") || (hotKeyList.Length > 10))
        {
            hotKeyList = "";
        }
        else
        {
            hotKeyList += currentKey;
        }

        if ((hotKeyList == "int") && (!adminLogin))
        {
            hotKeyList = "";
            adminLogin = true;
            AdminLoginEvn();
        }
    }

For the function above my hotkeys would be "Control (clears the string) + i + n + t" to fire my AdminLoginEvn method. The adminLogin boolean was incorporated so I only run my AdminLoginEvn one time while the application is open.

you have to remember the pressed keys (ie in a bool array). and set the position to 1 when its pressed (keydown) and 0 when up .

this way you can track more than one key. I suggest doing an array for special keys only

so you can do:

 if (e.KeyCode == Keys.Control)
 {
        keys[0] = true;
 }
// could do the same with alt/shift/... - or just rename keys[0] to ctrlPressed

if (keys[0] == true && e.KeyCode == Keys.Up)
 doyourstuff
Related