Scott Baker
Scott Baker

Reputation: 10443

AvalonEdit :: Ctrl + I KeyBinding Doesn't Work

I am using AvalonEdit to (surprise) create a text editor. I have added KeyBindings to the declaration:

<ae:TextEditor x:Name="TextEditor" ... >
   <ae:TextEditor.InputBindings>
      <KeyBinding Command="ToggleBold" Key="B" Modifiers="Control"/>
      <KeyBinding Command="ToggleItalic" Key="I" Modifiers ="Control"/>
      <!-- other bindings -->
   </ae:TextEditor.InputBindings>
</ae:TextEditor>

I have about twenty-ish buttons associated with the typical Commands and they're all working, including EditingCommands.ToggleItalic. I have KeyBindings associated with the commands and they all work as expected with the sole exception of Ctrl+I. I cannot get the Ctrl+I keybinding combination to work with any Command (tried using it with ToggleBold, for instance).

To be clear:

  1. The KeyBinding for ToggleItalic works if I bind to something that isn't Ctrl+I - Ctrl+Shift+I, for example, works perfectly.
  2. The Ctrl+I combination doesn't seem to work for any KeyBinding.

Anyone have any ideas as to why this might be? I don't want to deviate from the default KeyBindings - Ctrl+I for ToggleItalics is pretty ingrained for those of us that are fond of our keyboard shortcuts.

Upvotes: 1

Views: 1453

Answers (1)

Il Vic
Il Vic

Reputation: 5666

The Ctrl+I KeyGesture in the AvalonEdit control is already associated to the IndentSelection AvalonEditCommand (it is a RoutedCommand, so it can have one or more InputGestures).

If you take a look to the AvalonEditCommands class, you will find this code:

public static readonly RoutedCommand IndentSelection = new RoutedCommand( 
    "IndentSelection", typeof(TextEditor), 
    new InputGestureCollection { 
        new KeyGesture(Key.I, ModifierKeys.Control) 
});

So you have to remove the IndentSelection CommandBinding (in the EditingCommandHandler class) in order to use the Ctrl+I KeyGesture for another command.

EDIT I was thinking you can try to solve your problem by clearing the InputGestureCollection of the IndentSelection command in your Application.OnStartup method:

protected virtual void OnStartup(StartupEventArgs e)
{
    AvalonEditCommands.IndentSelection.InputGestures.Clear();
    /* If you want now you can add a new inputgesture */
    /* The rest of your code... */
}

I did not test this solution, but I suppose it can work.

Upvotes: 2

Related Questions