模拟退格按钮在c#中使用null ActiveSource

Tho*_*yme 2 c# keyboard-events uikeyboard

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

代码上下文:

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");
}
Run Code Online (Sandbox Code Playgroud)

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

Tho*_*yme 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);
}
Run Code Online (Sandbox Code Playgroud)