My goal is to replicate the keyboard chord combination from Visual Studio editor Ctrl E V which duplicates the line. To be clear, this is not a question on how to duplicate a line, but to handle a keyboard chord scenario in WPF.
How can a chord be accomplished where
- The CtrlV standalone won't fire the custom line duplication command and just not be handled when E has not accompanied it.
- AKA fall back to the system to do a Paste.
- This is not a deal breaker and I can process the clipboard if so.
I've tried
<KeyBinding Gesture="Ctrl E" Command="{Binding cmdSetChord_E}"/>
<KeyBinding Gesture="Ctrl V" Command="{Binding cmdDuplicateCurrent}"/>
and
bool ePressed;
private void tbPatternDesign_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.E)
ePressed = e.Handled = true;
if (ePressed && Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.V)
{
MessageBox.Show("Duplicate");
e.Handled = true;
ePressed = false;
}
}
to no avail.
CodePudding user response:
You can use CommandBinding
to override a control's default command implementation. This is a sample with a TextBox
.
<TextBox>
<TextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Paste" Executed="CommandBinding_Executed"/>
<CommandBinding Command="EditingCommands.AlignCenter" Executed="CommandBinding_Executed"/>
</TextBox.CommandBindings>
<TextBox.InputBindings>
<KeyBinding Command="EditingCommands.AlignCenter" Gesture="CTRL E"/>
</TextBox.InputBindings>
</TextBox>
bool ePressed;
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
switch (((RoutedUICommand)e.Command).Text)
{
case "Paste":
if (ePressed)
{
MessageBox.Show("Duplicate");
ePressed = false;
}
else
((TextBox)sender).Paste();
break;
case "AlignCenter":
ePressed = true;
break;
}
}
CodePudding user response:
How about:
Keyboard.IsKeyDown(Key.E) && Keyboard.IsKeyDown(Key.V) && ...
CodePudding user response:
Try using
Console.readkey()
It might not be the most intuitive way of doing it, but it is a good for-now solution.