How to create a KeyEventArgs object in WPF ( related to a SO answer )

前端 未结 5 1776
野趣味
野趣味 2020-12-29 04:39

I\'ve found this answer which look like what I need:

How can I programmatically generate keypress events in C#?

Except for the fact I can\'t create an insta

5条回答
  •  情书的邮戳
    2020-12-29 05:00

    If anyone is trying to create the KeyEventArgs for use in a unit test you'll find that Keyboard.PrimaryDevice.ActiveSource is null.. and throws an exception when you try to use it.

    Mocking a PresentationSource is a workable solution (requires sta):

    [Test]
    [RequiresSTA]
    public void test_something()
    {
      new KeyEventArgs(
        Keyboard.PrimaryDevice,
        new Mock().Object,
        0,
        Key.Back);
    }
    

提交回复
热议问题