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

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

We created KeyDown events, and also tried to add command bindings using input gestures, but we could not catch these two shortcuts.

+58
wpf tabs key-bindings ctrl
May 01 '09 at
source share
5 answers

What is your KeyDown handler? The code below works for me. One that causes me problems: Alt + Tab , but you did not ask for it: 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"); } } 
+91
May 01 '09 at
source share

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

 protected override void OnPreviewKeyDown(KeyEventArgs e) { if ((e.Key == Key.V || e.Key == Key.X || e.Key == Key.C) && Keyboard.IsKeyDown(Key.LeftCtrl)) return; } 
+29
May 25 '10 at 23:20
source share

You should use the KeyUp event, not KeyDown ...

+8
May 04 '09 at 16:35
source share

The working version of the Szymon Rozga answer (sorry, I can not comment). We do not look at Alt, but you can simply add its accounting 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"); } } 
+1
Nov 18 '16 at 12:40
source share

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 // } } 
0
May 17 '16 at 9:59 a.m.
source share



All Articles