如何在 WPF 中捕获 Ctrl + Tab 和 Ctrl + Shift + Tab?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/813389/
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-09-08 20:20:56  来源:igfitidea点击:

How to capture Ctrl + Tab and Ctrl + Shift + Tab in WPF?

wpftabskey-bindingsctrl

提问by FarrEver

What would be some sample code that will trap the Ctrl+Taband Ctrl+Shift+Tabfor a WPF application?

什么是一些示例代码,可以捕获WPF 应用程序的Ctrl+TabCtrl+ Shift+ Tab

We have created KeyDownevents and also tried adding command bindings with input gestures, but we were never able to trap these two shortcuts.

我们已经创建了KeyDown事件并尝试添加带有输入手势的命令绑定,但我们始终无法捕获这两个快捷方式。

回答by Szymon Rozga

What KeyDown handler did you have? The code below works for me. The one that gives me trouble is: Alt+ Tab, but you didn't ask for that :D

你有什么 KeyDown 处理程序?下面的代码对我有用。给我带来麻烦的是:Alt+ Tab,但您没有要求:D

public Window1()
{
   InitializeComponent();
   AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent);
}

private void HandleKeyDownEvent(object sender, KeyEventArgs e)
{
   if (e.Key == Key.Tab && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
   {
      MessageBox.Show("CTRL + SHIFT + TAB trapped");
   }

   if (e.Key == Key.Tab && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
   {
      MessageBox.Show("CTRL + TAB trapped");
   }
}

回答by mikeB

Gustavo's answer was exactly what I was looking for. We want to validate input keys, but still allow pasting:

古斯塔沃的回答正是我想要的。我们想要验证输入键,但仍然允许粘贴:

protected override void OnPreviewKeyDown(KeyEventArgs e)
{
   if ((e.Key == Key.V || e.Key == Key.X || e.Key == Key.C) && Keyboard.IsKeyDown(Key.LeftCtrl))
      return;
}

回答by Gus Cavalcanti

You have to use KeyUpevent, not KeyDown...

你必须使用KeyUp事件,而不是KeyDown......

回答by Lev

Working version of Szymon Rozgaanswer (sorry, I can't comment). We don't look on Alt, but it's accounting can be simply added at first if

Szymon Rozga答案的工作版本(抱歉,我无法发表评论)。我们不看 Alt,但它的会计可以简单地添加,如果

  public View()
  {
     InitializeComponent();
     AddHandler(Keyboard.PreviewKeyDownEvent, (KeyEventHandler)controlKeyDownEvent);
  }

  private void controlKeyDownEvent(object sender, KeyEventArgs e)
  {
     if (e.Key == Key.Tab && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
     {
        if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift))
           MessageBox.Show("CTRL + SHIFT + TAB trapped");
        else
           MessageBox.Show("CTRL + TAB trapped");
     }
  }

回答by luka

Hi you can use this on keydown event

嗨,您可以在 keydown 事件上使用它

 private void OnButtonKeyDown(object sender, KeyEventArgs e)
    {
        if(Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.Tab) && Keyboard.IsKeyDown(Key.LeftShift))
        {
           //
           // TODO: somthing here
           //
        }
    }