如何在C#中使用多个修饰键

jsm*_*ith 24 c# keyboard-shortcuts key keydown modifier-key

我正在使用keydown事件来检测按下的键,并为各种操作提供了几个键组合.

if (e.KeyCode == Keys.C && e.Modifiers == Keys.Control && e.Modifiers == Keys.Shift)
{
    //Do work
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
    //Paste
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我点击Ctrl+ Shift+ 的键组合C不起作用.我已经重新订购了它们,并将它置于顶部,认为它可能是来自Ctrl+的干扰C,甚至删除Ctrl+ C以查看它是否导致问题.它仍然无法正常工作.我知道它可能非常简单,但不能完全理解它是什么.我的所有1修饰符+ 1组合键都可以正常工作,只要我添加第二个修饰符就是它不再有效.

Rom*_*Rom 42

if (e.KeyCode == Keys.C && e.Modifiers == (Keys.Control | Keys.Shift))
{
    //Do work
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
    //Paste
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*s J 7

你试过e.Modifiers == (Keys.Control | Keys.Shift)吗?


JDu*_*ley 6

如果你要允许CtrlShift再使用按位OR(作为Keys一个Flags枚举)

if (e.KeyCode == Keys.C && e.Modifiers == (Keys.Control | Keys.Shift))
{
    //Do work (if Ctrl-Shift-C is pressed, but not if Alt is pressed as well)
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
    //Paste (if Ctrl is only modifier pressed)
}
Run Code Online (Sandbox Code Playgroud)

如果Alt按下也会失败