如何在 C# 中使用多个修饰键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1434867/
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
How to use multiple modifier keys in C#
提问by jsmith
I am using a keydown event to detect keys pressed and have several key combinations for various operations.
我正在使用 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
}
For some reason the key combination in which I hit Ctrl+ Shift+ Cis not working. I have re ordered them, and placed it at the top thinking it might be interference from the Ctrl+ C, and even removed the Ctrl+ Cto see if it was causing a problem. It still does not work. I know it's probably something very simple, but can't quite grasp what it is. All of my 1 modifier + 1 key combination's work fine, as soon as I add a second modifier is when it no longer works.
出于某种原因,我点击Ctrl+ Shift+ 的组合键C不起作用。我重新订购了它们,并将其放在顶部,认为它可能是来自Ctrl+ 的干扰C,甚至删除了Ctrl+C以查看它是否导致问题。它仍然不起作用。我知道这可能是非常简单的事情,但不能完全理解它是什么。我所有的 1 个修饰符 + 1 个组合键都可以正常工作,一旦我添加第二个修饰符,它就不再起作用了。
采纳答案by Rom
if (e.KeyCode == Keys.C && e.Modifiers == (Keys.Control | Keys.Shift))
{
//Do work
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
//Paste
}
回答by Chris J
Have you tried e.Modifiers == (Keys.Control | Keys.Shift)
?
你试过e.Modifiers == (Keys.Control | Keys.Shift)
吗?
回答by Donut
Try this. Should behave the way you want it to, and it's a little simpler.
尝试这个。应该按照您希望的方式行事,而且要简单一些。
if (e.Control)
{
if (e.Shift && e.KeyCode == Keys.C)
{
//Do work
}
else if (e.KeyCode == Keys.V)
{
//Paste
}
}
回答by JDunkerley
If you want to allow Ctrland Shiftthen use the bitwise OR (as Keys
is a Flags
enum)
如果你要允许Ctrl和Shift再使用按位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)
}
This will fail if Altis pressed as well
如果Alt也按下,这将失败
回答by Druid
Another way would be to add an invisible menu item, assign the Ctrl+ Shift+ Cshortcut to it, and handle the event there.
另一种方法是添加一个不可见的菜单项,为其分配Ctrl+ Shift+C快捷方式,并在那里处理事件。
回答by Chris
This is what I did for a Ctrl+ZUndo and Ctrl+Shift+ZRedo operation and it worked.
这就是我为Ctrl+ ZUndo 和Ctrl+ Shift+ ZRedo 操作所做的,并且奏效了。
Private Sub Form_Main_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Add
diagramView.ZoomIn()
Case Keys.Subtract
diagramView.ZoomOut()
Case Keys.Z
If e.Modifiers = Keys.Control + Keys.Shift Then
diagram.UndoManager.Redo()
ElseIf e.Modifiers = Keys.Control Then
diagram.UndoManager.Undo()
End If
End Select
End Sub
回答by Stephane Ehret
if ((Keyboard.Modifiers & ModifierKeys.Shift | ModifierKeys.Control) > 0)
Debugger.Launch();
回答by Martin
Seeing as no one else mentions them, i'm just going to leave the suggestion to use KeyEventArgs.KeyData:
看到没有其他人提到它们,我只想留下使用 KeyEventArgs.KeyData 的建议:
if (e.KeyData == (Keys.C | Keys.Control | Keys.Shift)
{
//do stuff
//potentially use e.Handled = true
}
if (e.KeyData == (Keys.V | Keys.Control)
{
//do other stuff
//potentially use e.Handled = true
}
This should only act on specific key combinations, though the order of the modifiers don't seem to matter, the first one is always the last pressed key.
这应该只作用于特定的组合键,虽然修饰符的顺序似乎并不重要,但第一个总是最后按下的键。
And e.Handled = true should stop it, though i don't know the specific mechanics behind it.
并且 e.Handled = true 应该停止它,尽管我不知道它背后的具体机制。