Capture Ctrl-X with text field KeyDown event in WPF

I am trying to raise an event when the user presses ctrl - x using the KeyDown . This works fine for ctrl - D , but the event does not fire when ctrl - x is pressed. I assume this is because ctrl - x is a cut command. Is there a way to trigger an event when ctrl - x is pressed?

 private void textBox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)) { switch (e.Key) { case Key.D: //handle D key break; case Key.X: //handle X key break; } } } 
+6
source share
3 answers

You can override the existing cut command:

 <TextBox> <TextBox.InputBindings> <KeyBinding Key="X" Modifiers="Control" Command="{Binding TestCommand}" /> </TextBox.InputBindings> </TextBox> 

You need to create a team.

+6
source

To do this in wpf, I will try the following:

 private void HandleKeyDownEvent(object sender, KeyEventArgs e) { if (e.Key == Key.X && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) { MessageBox.Show("You press Ctrl+X :)"); } } 
+10
source

try the following in keydown event

  if (e.Control == true && e.KeyCode==keys.x) { e.Handled = true; textBox1.SelectionLength = 0; //Call your method } 
-1
source

Source: https://habr.com/ru/post/901159/


All Articles