【问题标题】:WPF KeyEventArgs second argument is nullWPF KeyEventArgs 第二个参数为空
【发布时间】:2014-06-08 15:26:53
【问题描述】:
KeyEventArgs e = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, keyData);

Keyboard.PrimaryDevice.ActiveSource 为空,程序停止工作。我也试过了:

MainWindow mw = new MainWindow();
System.Windows.PresentationSource source = System.Windows.PresentationSource.FromVisual(mw);
KeyEventArgs e = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, keyData);

但仍然为空,我无法将 mw 替换为此,因为代码位于不同的文件类中。

我正在尝试从其他窗口捕捉键盘,代码只有在我用程序的当前活动窗口捕捉键盘时才有效。

有什么想法吗?

【问题讨论】:

    标签: wpf keyboard keyeventargs


    【解决方案1】:

    它为空,因为您的应用程序没有焦点。您可以创建一个虚拟System.Windows.Interop.HwndSource,如下所示:

    KeyEventArgs kea = new KeyEventArgs(Keyboard.PrimaryDevice, new HwndSource(0, 0, 0, 0, 0, "", IntPtr.Zero), 0, key);
    

    【讨论】:

    • 您不需要处理 HwndSource 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多