[英]Simulating a Backspace button press in c# with a null ActiveSource
上下文:我們正在觸摸屏亭上使用屏幕鍵盤,以允許用戶輸入文本。 退格按鈕失敗,因為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與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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.