In WPF, to use shortcuts, you need to focus the appropriate controller. However, with the InputManager you can capture all kinds of inputs from your application. Here you do not need to focus on the appropriate controller.
First you need to sign the event.
InputManager.Current.PreProcessInput -= Current_PreProcessInput; InputManager.Current.PreProcessInput += Current_PreProcessInput;
Then
private void Current_PreProcessInput(object sender, PreProcessInputEventArgs args) { try { if (args != null && args.StagingItem != null && args.StagingItem.Input != null) { InputEventArgs inputEvent = args.StagingItem.Input; if (inputEvent is KeyboardEventArgs) { KeyboardEventArgs k = inputEvent as KeyboardEventArgs; RoutedEvent r = k.RoutedEvent; KeyEventArgs keyEvent = k as KeyEventArgs; if (r == Keyboard.KeyDownEvent) { } if (r == Keyboard.KeyUpEvent) { } } } } catch (Exception ex) { } }
Similarly, you can filter out all unnecessary things and get the required input. Since this is a shortcut capture app, I used the KeyDown event and KeyUp event.
You can also get all the details of a keystroke.
keyEvent.Key.ToString()
Vinusha perera
source share