Detecting multiple simultaneous keypresses in C#

十年热恋 提交于 2019-12-06 10:02:12

If you're looking for regular keys then you can store them in a list: On KeyDown, add the key to a list. On Key Up, remove it from the list. On KeyDown, check what's in the list.

However, I'm not sure that there are keydown/keyup events for modifier keys like ctrl, shift, alt. For those you can do something like this:

bool CtrlDown = ((e.Modifiers & Keys.Control) > 0);
bool CtrlOnlyModifierDown = ((e.ModifierKeys & Keys.Control) == Keys.Control) 

e.KeyCode contains the key value + modifier info

e.KeyCode = e.KeyValue | e.Modifiers

Use e.KeyCode

Not sure if you have had any luck.

But try this code:

        switch (e.KeyData)
        {
            case Keys.Control:
                {
                    if (e.KeyData == Keys.Subtract) 
                    { }
                    else if (e.KeyData == Keys.C) 
                    { }
                    break;
                }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!