KeyDown : recognizing multiple keys

2019-01-14 01:03发布

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

标签: c# keydown ctrl
9条回答
乱世女痞
2楼-- · 2019-01-14 01:36

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

查看更多
虎瘦雄心在
3楼-- · 2019-01-14 01:37

this will work for sure. Be careful to handle KeyUp event and not keyDown.

private void mainForm_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
        {
             //insert here
        }
    }

For me, keyDown didn't work, keyUp worked instead for the same code.

I don't know why, but it seems because keyDown event happens directly after you press any key, even if that was ctrl key, so if you pressed ctrl+Up you will press ctrl key before the UP key and thus the event will occur before you can press the other, also pressing the second key will triggers the event again.

While using KeyUp will not trigger the event until you release the key, so you can press ctrl, and the press the second key, which will trigger one event.

查看更多
倾城 Initia
4楼-- · 2019-01-14 01:38

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
}
查看更多
SAY GOODBYE
5楼-- · 2019-01-14 01:47

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

查看更多
趁早两清
6楼-- · 2019-01-14 01:52

From the MSDN page on KeyEventArgs:

if (e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
{
    //Do stuff...
}
查看更多
Luminary・发光体
7楼-- · 2019-01-14 01:53

You can try using the Keyboard object to detect the IsKeyDown property. Also, if you don't want the browser shortcut to over-ride you can set Handled property to true.But be careful when over-riding browser shortcuts as it could cause confusion.

private void Page_KeyDown(object sender, KeyEventArgs e)
{
    // If leftCtrl + T is pressed autofill username
    if (Keyboard.IsKeyDown(Key.T) && Keyboard.IsKeyDown(Key.LeftCtrl))
    {
        txtUser.Text = "My AutoFilled UserName";
        e.Handled = true;
    }
}
查看更多
登录 后发表回答