上下文:我们在触摸屏信息亭上使用屏幕键盘来允许用户输入文本。退格按钮失败,因为 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/