簡體   English   中英

WPF / C#鍵盤事件輸入sendkey

[英]WPF/C# keyboard event input sendkey

嗨,我想通過鍵盤的sendind鍵在文本框中鍵入文本。

Xaml:

<Button Name="Start" Margin="20" Grid.Column="0" Grid.Row="0" VerticalAlignment="Top" Click="Start_Click">Start</Button>
<TextBox Name="test" Grid.Column="0" Grid.Row="2"></TextBox>

C#:

private async void Start_Click(object sender, RoutedEventArgs e)
{
 Keyboard.Focus(test);
 await wait(1);
 SendKeys.Send("s"); 
}

但這給我一個錯誤。 我做錯了什么?

我認為問題在於您正在使用Windows窗體技術“ SendKeys”與WPF組件(而非Windows窗體技術)進行交互。 您需要像這樣將事件發送到TextBox:

TextCompositionManager.StartComposition(new TextComposition(InputManager.Current, test, "s"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM