【问题标题】:WPF/C# keyboard event input sendkeyWPF/C# 键盘事件输入 sendkey
【发布时间】:2013-03-21 14:25:12
【问题描述】:

您好,我想通过键盘上的 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"); 
}

但这给了我一个错误。我做错了什么?

【问题讨论】:

  • 它给出了什么错误?它通常说是什么问题
  • InvalidOperationException 被用户代码取消。
  • 异常的错误信息是什么?

标签: c# wpf keyboard sendkeys


【解决方案1】:

我认为问题在于您使用的是“SendKeys”,它是 Windows 窗体技术与 WPF 组件交互,这不是 Windows 窗体技术。您需要像这样向 TextBox 发送事件:

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

【讨论】:

    猜你喜欢
    • 2014-12-28
    • 1970-01-01
    • 2021-12-26
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2017-05-29
    • 2021-10-25
    相关资源
    最近更新 更多