开发者

Simulating a Backspace button press in c# with a null ActiveSource

开发者 https://www.devze.com 2023-01-14 04:36 出处:网络
Context: We\'re using an on-screen keyboard on a touch screen kiosk to allow users to enter text.The backspace button is failing because the System.Windows.Input.Keyboard.PrimaryDevice.ActiveSource be

Context: We're using an on-screen keyboard on a touch screen kiosk to allow users to enter text. The backspace button is failing because the System.Windows.Input.Keyboard.PrimaryDevice.ActiveSource becomes null.

Code Context:

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");
}

I can't use a开发者_运维技巧 KeyEventArgs with a null ActiveSource, and System.Windows.Forms.SendKeys.SendWait("{BACKSPACE}") doesn't work either.


I just spoofed the source to fix it like so:

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号