如何创建在WPF中KeyEventArgs对象(涉及到一个SO回答) [英] How to create a KeyEventArgs object in WPF ( related to a SO answer )

查看:398
本文介绍了如何创建在WPF中KeyEventArgs对象(涉及到一个SO回答)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个答案看起来像什么,我需要:

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

如何编程生成C#按键事件?

除了一个事实,我无法创建 KeyEventArgs 的一个实例(我不知道如何)

Except for the fact I can't create an instance of KeyEventArgs ( I don't know how )

有问题的代码是:

 var key = Key.Insert;                    // Key to send
  var target = Keyboard.FocusedElement;    // Target element
  var routedEvent = Keyboard.KeyDownEvent; // Event to send

  target.RaiseEvent(
    new KeyEventArgs(
      Keyboard.PrimaryDevice,
      PresentationSource.FromVisual(target), //<--- HERE, I can't
      0,
      key)
    { RoutedEvent=routedEvent }
  );



编译器说:

The compiler says:

The best overloaded method match for
'System.Windows.PresentationSource.FromDependencyObject(System.Windows.DependencyObject)'
 has some invalid arguments

在IDE说:

参数类型 IInputElement 不是分配给参数类型的DependencyObject

Argument type IInputElement is not assignable to parameter type DependencyObject

和跨计算器,我发现了几个答案指挥到答案,但他们没有解决如何在首位的实例。

And across StackOverflow I've found several answers directing to that answer but none of them address how to create the instance in first place.

我怎么能这样做?

推荐答案

phewwww

我发现它: Keyboard.PrimaryDevice.ActiveSource 已被使用。

InputManager.Current.ProcessInput(
    new KeyEventArgs(Keyboard.PrimaryDevice,
        Keyboard.PrimaryDevice.ActiveSource,
        0, Key.Tab)
    {
       RoutedEvent = Keyboard.KeyDownEvent
    }
);

这篇关于如何创建在WPF中KeyEventArgs对象(涉及到一个SO回答)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆