Command Routing for Keyboard Shortcuts

Basically, I want to create a keyboard shortcut that is valid within the window area, and not just turned on when the focus is inside the control that links it.

in details....

I have a window with three controls:

  • toolbar
  • text field
  • User control

There is a button on the toolbar attached to the CustomCommands.CmdA command and associated with the Ctrl + T key combination.

My user management can handle CmdA . When I launch the application and click on my CmdA user control, it works and works fine. Also, Ctrl + T causes the command to fire.

However, when I select a text box, my custom CmdA command CmdA disabled.

I can fix this by setting the command target for the CmdA button. Now when I select textBox, CmdA is still on.

But the keyboard shortcut Ctrl + T does nothing.

Is there an easy way to zoom out on keyboard shortcuts? Or do I need to catch a keystroke somewhere below, and determine which command it is associated with and direct it myself (if so, in which environment can this be done?)

+4
source share
1 answer

To do this, you just need to specify input bindings in the window, for example:

  <Window.InputBindings> <KeyBinding Key="N" Modifiers="Control" Command="New"/> ... </Window.InputBindings> 

For application built-in commands (which are RoutedCommands ) you need CommandBinding in the window.

 <Window.CommandBindings> <CommandBinding Command="New" Executed="CommandBinding_Executed" /> ... </Window.CommandBindings> 
+2
source

Source: https://habr.com/ru/post/1334842/


All Articles