C# KeyDown:识别多个键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1265634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:25:19  来源:igfitidea点击:

KeyDown : recognizing multiple keys

c#keydownctrl

提问by

How can I determine in KeyDownthat CtrlUpwas pressed.

我怎么能确定在KeyDown那个CtrlUp被按下。

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 Ctrland then the other one...

无法工作,因为永远不会在同一秒内完全按下两个键。你总是先是Ctrl然后另一个......

回答by Niko

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 .

您必须记住按下的键(即在 bool 数组中)。并在按下时将位置设置为 1 (keydown) 并在按下时将位置设置为 0 。

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

回答by Garry Shutler

You can check the modifiers of the KeyEventArgs like so:

您可以像这样检查 KeyEventArgs 的修饰符:

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

MSDN reference

MSDN参考

回答by Matt Hamilton

From the MSDN page on KeyEventArgs:

KeyEventArgs 上MSDN 页面

if (e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
{
    //Do stuff...
}

回答by IordanTanev

In the KeyEventArgsthere are properties Ctrl, Altand Shiftthat shows if these buttons are pressed.

KeyEventArgs有属性CtrlAltShift显示这些按钮是否被按下。

回答by Fredrik M?rk

You can use the ModifierKeys property:

您可以使用 ModifierKeys 属性:

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 CTRLwas pressed regardless of the state of the SHIFTor ALTkeys, 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:

请注意,ModifierKeys 值可以是值的组合,因此如果您想检测CTRL是否按下了SHIFTALT键的状态,您将需要像上面的示例一样执行按位比较。如果你想确保没有其他修饰符被按下,你应该检查是否相等:

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

回答by zulumojo

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

您可以尝试使用Keyboard对象来检测IsKeyDown属性。此外,如果您不希望浏览器快捷方式被覆盖,您可以将Handled属性设置为true。但在覆盖浏览器快捷方式时要小心,因为它可能会导致混淆。

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;
    }
}

回答by A. Harkous

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

这肯定会起作用。小心处理KeyUpevent 而不是keyDown.

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

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

对我来说,keyDown没有用,keyUp 代替了相同的代码。

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

我不知道为什么,但似乎是因为keyDown事件在您按下任意键后直接发生,即使那是ctrl键,所以如果您按下ctrl+,Up您将在ctrl键之前按下UP键,因此事件将在您按下另一个键之前发生,同样按下第二个键将再次触发事件。

While using KeyUpwill 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.

使用KeyUpctrl只有松开按键才会触发事件,所以可以按,再按第二个键,会触发一个事件。

回答by jordyan

you can try my working code :

你可以试试我的工作代码:

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

i use this code because i don't know why when i use :

我使用此代码是因为我不知道为什么使用时:

(e.Keycode == Keys>up && e.Alt==true)

didn't work.

没有用。

回答by Siegfried.V

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 :

仅当您先按 LeftCtrl,然后按“UP”时,此代码才有效。如果顺序不重要,我推荐一个:

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.

在这种情况下,两个 Ctrl 都被考虑在内,并且顺序无关紧要。

回答by Moseyza

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...");
        }
    }