如何捕获 Ctrl + Tab 和 Ctrl + 移位+ WPF 中的选项卡?

发布于 2024-07-18 07:48:34 字数 187 浏览 7 评论 0原文

捕获 Ctrl+TabCtrl+Shift+Tab 的示例代码是什么 对于 WPF 应用程序?

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

What would be some sample code that will trap the Ctrl+Tab and Ctrl+Shift+Tab for a WPF application?

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

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

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

发布评论

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

评论(5

心碎的声音 2024-07-25 07:48:35

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

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

Hi you can use this on keydown event

 private void OnButtonKeyDown(object sender, KeyEventArgs e)
    {
        if(Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.Tab) && Keyboard.IsKeyDown(Key.LeftShift))
        {
           //
           // TODO: somthing here
           //
        }
    }
叫嚣ゝ 2024-07-25 07:48:34

你有什么 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");
   }
}

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

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");
   }
}
转身泪倾城 2024-07-25 07:48:34

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

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

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;
}
情话已封尘 2024-07-25 07:48:34

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

You have to use KeyUp event, not KeyDown...

执笏见 2024-07-25 07:48:34

Szymon Rozga 答案的工作版本(抱歉,我无法发表评论)。 我们不看Alt,但是它的记账可以简单地先添加if

  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");
     }
  }

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

  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");
     }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文