'How to simulate a key kombination in WPF unit test?

I'm trying to write a unit test for a WPF component. How is it possible to simulate a key combination (not only 1 key)?

Simulating to press 1 key is easy:

        var window = new Window();

        var keyEventArgs = new KeyEventArgs(Keyboard.PrimaryDevice, Substitute.For<PresentationSource>(), 0, Key.E)
        {
            RoutedEvent = Keyboard.KeyDownEvent,
        };

        window.RaiseEvent(keyEventArgs);

Finding out if a specific key combination was used (e.g. Ctrl+Alt+Shift+E):

if(EventArgs.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt | ModifierKeys.Control | ModifierKeys.Shift)
   && EventArgs.Key == Key.E)

We see: It's possible to access the modifiers via EventArgs.KeyboardDevice.

But how can we set them?

I experimented a lot. I tried to mock the first argument of the KeyEventArgs constructor, but without success.

How is it possible to simulate a key combination?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source