KeyDown : recognizing multiple keys

后端 未结 13 2772
悲&欢浪女
悲&欢浪女 2020-12-03 02:47

How can I determine in KeyDown that CtrlUp was pressed.

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
           


        
13条回答
  •  忘掉有多难
    2020-12-03 03:00

    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.

提交回复
热议问题