c# - 使用空 ActiveSource 在 C# 中模拟按 Backspace 按钮

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

我无法将 KeyEventArgs 与 null ActiveSource 一起使用,并且 System.Windows.Forms.SendKeys.SendWait("{BACKSPACE}") 也不起作用。

最佳答案

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

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

关于c# - 使用空 ActiveSource 在 C# 中模拟按 Backspace 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3640128/

相关文章:

c# - XAML TextBox isReadOnly 绑定(bind)

c# - KeyDown 事件不是从网格中引发的

Angular,如何阻止与父组件的键盘交互

ios - UIKeyboardWillShowNotification & UIKeyboardDidShowNotification 报错键盘高度

C# 抽象方法

c# - WP8 : Local Storage or Isolated Storage to store data?

c# - ASP.Net MVC Web Api 函数为完全不同的调用返回错误

ios - 检测国际键盘的出现和消失

iOS:在 iPad 模态视图中获取键盘点的顶部?

ios - becomeFirstResponder 正在触发 UIKeyboardDidHideNotification