0

I want to be able to allow users to change hotkeys at runtime.

Does anyone have a concrete example on how to bind a key in codebehind c# to accomplish this?

I assume I have to do a keybinding with a RoutedCommand. Assume that I want to push a button on the UI with the hotkey.

eg... Let the user push F5 to click the btnGo on the app.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115

1 Answers1

0

The way that commands work is a bit different from your thinking. When you press a button it doesn't fire the click for a button in your app. Rather, you define a command (in this case, you could have a command called "Go") and you can assign this command to various elements in your application (menu items, buttons, etc.) All of these items fire the command and the command has an "Executed" function which performs the code.

If you add CommandBindings to your main window then you can use hotkeys to fire those commands globally. You can easily place command bindings in XAML by such code as:

<Window.CommandBindings>
    <CommandBinding Command="local:MyCommands.Go" CanExecute="GoCanExecute" Executed="Go" />
</Window.CommandBindings>

To create your own command you can create a "RoutedUICommand" in code and set the default members (default name, default keybind, etc.). You most likely want to make it a static property of a class.

Then if you want to change or add keybinds for this command you can just access this static property and modify the InputGestures collection. You can clear it, add new key bindings, and change them as necessary.

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
  • Turns out I was able to leave the command binding in the xaml and just define a keygesture and use inputbinding to add it. So... it works however.... what do people normally do for what is in the click code such as greying out a button. Is all this code pulled out of the click code and instead done from codebehind? – user1435769 Jun 06 '12 at 15:12
  • For controls that support commands they will automatically be disabled according to the "CanExecute" property of the command binding. CanExecute is a function you write that should set the e.CanExecute property to true if your function can be executed at that time, false if not. – Trevor Elliott Jun 06 '12 at 15:21
  • The code I was given has a CanExecute function. Problem is that there are other things such as generation of a filename that is done in the click event that is later used by the code called by the command. Is there a way to call click event when the hotkey command is fired or is this an impossible/or bad way of doing things? – user1435769 Jun 06 '12 at 20:31
  • Calling a click event from a hotkey press is a bad way of doing things. Why doesn't the Button just use the Command binding? Once the Executed event handler gets called it shouldn't care whether it was fired from a hotkey or a button click. – Trevor Elliott Jun 06 '12 at 21:13
  • I didn't like that idea either.... I wasn't aware of the onExecute. So... I can take my click code and put it in there. – user1435769 Jun 07 '12 at 16:49