2

I wish I could define any sort of shortcut like Ctrl + F, Ctrl+P, Ctrl+Alt+Tab to call a method. I tried to use CommandBinding and KeyBinding without any success. If I ain't wrong, the only way would use the CanExecute or Executed of CommandBinding to do that, but I didn't know how to associate that with any custom shortcut that I wanted, and it's necessary to define the Command with, for example, ApplicationCommands.Open.

It would be perfect if I could simply define the shortcuts like Key="B" and Modifiers="Control" with the command Command="SomeEventHandlerHere", but it isn't going that simple, unfortunately.

EDIT

I've tried this so far (it looks so wrong even to me):

CommandBinding cb = new CommandBinding(ApplicationCommands.NotACommand, MyMethod);
KeyGesture kg = new KeyGesture(Key.B, ModifierKeys.Control);
KeyBinding kb = new KeyBinding(ApplicationCommands.NotACommand, kg);
this.InputBindings.Add(kb);

private void MyMethod(object sender, ExecutedRoutedEventArgs e)
{
    // Do something
}
xav
  • 5,452
  • 7
  • 48
  • 57
Noberto Pessôa
  • 125
  • 3
  • 12
  • 1
    Its almost that simple. Why dont you post the code that you've created so far and we'll see how close you really are. – crthompson Apr 07 '14 at 21:35
  • It looks like your question is very similar to this one: http://stackoverflow.com/questions/1361350/keyboard-shortcuts-in-wpf – Sean B Apr 07 '14 at 21:48
  • It really seems to be the same question. But could you help me about this line?: `Command="{x:Static local:MyWindow.MyCommand}`. I'm getting an error: `The namespace prefix "local" is not defined`. – Noberto Pessôa Apr 07 '14 at 22:08

1 Answers1

0

I've just found what I was looking for.

In order to create my own command (instead of using pre-existing ones like "Open", "Help", "Save", etc), I need to create a new RoutedUICommand. Next, we create a CommandBinding to associating the Command with the methods.

<Window.Resources>
    <RoutedUICommand x:Key="MyCustomCommand"/>
</Window.Resources>

<Window.CommandBindings>
    <CommandBinding Command="{StaticResource MyCustomCommand}" Executed="CommandExecutedMethod" CanExecute="CommandCanExecuteMethod"/>
</Window.CommandBindings>  

In the code behind we have:

private void CommandCanExecuteMethod(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
    e.Handled = true;
}

private void CommandExecutedMethod(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Command executed");
    e.Handled = true;
}  

Now I can do what I was looking for:

<Window.InputBindings>
    <KeyBinding Key="G" Modifiers="Control" Command="{StaticResource MyCustomCommand}"/>
</Window.InputBindings>

With the above, if the window is focused, when we press Ctrl + G, the method CommandExecutedMethod is called.

We also can use the command like this:

<Button Content="Click me" Command="{StaticResource MyCustomCommand}" />  

Fonts: Tech.Pro StackOverflow

Community
  • 1
  • 1
Noberto Pessôa
  • 125
  • 3
  • 12