views:

10

answers:

1

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.

A: 

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