I'm trying to trigger an event when the user presses ctrl-x using the KeyDown
event. This works fine for ctrl-D but the event doesn't trigger when ctrl-x is pressed. I'm guessing this is because ctrl-x is the "cut" command. Is there any 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;
}
}
}
To do that in wpf I try this:
private void HandleKeyDownEvent(object sender, KeyEventArgs e)
{
if (e.Key == Key.X && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
MessageBox.Show("You press Ctrl+X :)");
}
}
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 command though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With