2

背景:我们在触摸屏信息亭上使用屏幕键盘来允许用户输入文本。退格按钮失败,因为 System.Windows.Input.Keyboard.PrimaryDevice.ActiveSource 变为空。

代码上下文:

if (System.Windows.Input.Keyboard.PrimaryDevice.ActiveSource != null)
{
    System.Windows.Input.KeyEventArgs ke = 
        new System.Windows.Input.KeyEventArgs(
            System.Windows.Input.Keyboard.PrimaryDevice, 
            System.Windows.Input.Keyboard.PrimaryDevice.ActiveSource,
            0,
            System.Windows.Input.Key.Back);
    ke.RoutedEvent = UIElement.KeyDownEvent;
    System.Windows.Input.InputManager.Current.ProcessInput(ke);
}
else
{
    Console.Out.WriteLine("Problemo");
}

我不能使用带有 null ActiveSource 的 KeyEventArgs,并且 System.Windows.Forms.SendKeys.SendWait("{BACKSPACE}") 也不起作用。

4

1 回答 1

6

我只是欺骗了源来修复它,如下所示:

else
{
    //Hacky?  Perhaps... but it works.
    PresentationSource source = PresentationSource.FromVisual(this);
    KeyEventArgs ke = new KeyEventArgs(
                        Keyboard.PrimaryDevice,
                        source,
                        0,
                        System.Windows.Input.Key.Back);
    ke.RoutedEvent = UIElement.KeyDownEvent;
    System.Windows.Input.InputManager.Current.ProcessInput(ke);
}
于 2010-09-03T23:41:34.980 回答