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

发布于 08-05 03:23 字数 538 浏览 12 评论 0原文

我正在使用 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
}

由于某种原因,我按下 Ctrl + Shift + C 的组合键不起作用。我重新排序了它们,并将其放在顶部,认为可能是 Ctrl + C 的干扰,甚至删除了 Ctrl + C 查看它是否导致了问题。它仍然不起作用。我知道这可能很简单,但无法完全理解它是什么。我的所有 1 个修饰符 + 1 个组合键都工作正常,一旦我添加第二个修饰符,它就不再起作用。

I am using a keydown event to detect keys pressed and have several key combinations for various operations.

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 + C is 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 + C to 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一直在等你来2024-08-12 03:23:45
if (e.KeyCode == Keys.C && e.Modifiers == (Keys.Control | Keys.Shift))
{
    //Do work
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
    //Paste
}
if (e.KeyCode == Keys.C && e.Modifiers == (Keys.Control | Keys.Shift))
{
    //Do work
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
    //Paste
}
清音悠歌2024-08-12 03:23:45

您是否尝试过e.Modifiers == (Keys.Control | Keys.Shift)

Have you tried e.Modifiers == (Keys.Control | Keys.Shift)?

萌辣2024-08-12 03:23:45

如果您想允许 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)
}

)如果同时按下 Alt 也会失败

If you want to allow Ctrl and Shift then use the bitwise OR (as Keys is a Flags enum)

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 Alt is pressed as well

没有伤那来痛2024-08-12 03:23:45
      if ((Keyboard.Modifiers & ModifierKeys.Shift | ModifierKeys.Control) > 0)
          Debugger.Launch();
      if ((Keyboard.Modifiers & ModifierKeys.Shift | ModifierKeys.Control) > 0)
          Debugger.Launch();
万劫不复2024-08-12 03:23:45

另一种方法是添加一个不可见的菜单项,为其分配 Ctrl + Shift + C 快捷键,并在那里处理事件。

Another way would be to add an invisible menu item, assign the Ctrl + Shift + C shortcut to it, and handle the event there.

倾其所爱2024-08-12 03:23:45

这就是我对 Ctrl+Z 撤消和 Ctrl+Shift+Z 所做的操作kbd> 重做操作,它起作用了。

  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

This is what I did for a Ctrl+Z Undo and Ctrl+Shift+Z Redo operation and it worked.

  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
三人与歌2024-08-12 03:23:45

试试这个。应该按照您想要的方式运行,而且更简单一些。

 if (e.Control)
 {
    if (e.Shift && e.KeyCode == Keys.C)
    {
       //Do work
    }
    else if (e.KeyCode == Keys.V)
    {
       //Paste
    }
 }

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
    }
 }
俯瞰星空2024-08-12 03:23:45

鉴于没有其他人提到它们,我只是建议使用 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
}

这应该只对特定的组合键起作用,尽管修饰符的顺序似乎并不重要,第一个总是最后按下的键。

e.Handled = true 应该阻止它,尽管我不知道它背后的具体机制。

Seeing as no one else mentions them, i'm just going to leave the suggestion to use 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.

痴情换悲伤2024-08-12 03:23:45

如果您有更多案例并且更易于阅读,另一种更合适的方法是:

private void Form_KeyDown(object sender, KeyEventArgs e)
{
    Action action = e.KeyData switch
    {
      (Keys.Control | Keys.Shift | Keys.C) => DoWork,
      (Keys.Control | Keys.V) => Paste,
      _ => null
    };
    action?.Invoke();
}
private void DoWork() 
{ // do work }
private void Paste() 
{ // paste }

Another approach more suitable if you have more cases, and easier to read is:

private void Form_KeyDown(object sender, KeyEventArgs e)
{
    Action action = e.KeyData switch
    {
      (Keys.Control | Keys.Shift | Keys.C) => DoWork,
      (Keys.Control | Keys.V) => Paste,
      _ => null
    };
    action?.Invoke();
}
private void DoWork() 
{ // do work }
private void Paste() 
{ // paste }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文