I have a button on a XAML form that calls the btnName_Click() code in the backing cs file.
I also want to assign a keyboard shortcut to run that same code. Basically CTRL + ← or something similar.
This needs to work regardless of the control I’m currently in (there’s a TextBox for example that I want to ensure doesn’t capture the event if I’m in there).
I’ve read up on routed commands but that seems like a lot of work for something which should be simple.
Is there an easy way to do this or do I need to create routed commands? If I do need to use them, what’s the simplest way to achieve what I want?
Have your XAML as:
and your cs as:
You should put the
PreviewKeydownevent on the topmost control like your window so that once Ctrl + ← is hit only the window or top control handles it and no one else.PreviewKeyDownis a tunneled event – it originates at the topmost control and goes down to actual control.