wpf - WPF将密钥发送到文本框
问题描述
我正在使用虚拟键盘,但我很难发送按键。什么都没有发生,除非我将它们硬添加到文本框中。这是我的代码
public static void Send(Key key)
{ //Found online
var e = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, key)
{
RoutedEvent = Keyboard.KeyDownEvent
};
InputManager.Current.ProcessInput(e);
}
private void ClickOnKey(object sender, RoutedEventArgs e)
{
CustomButton cb = sender as CustomButton;
string text = cb.text;
Console.WriteLine(text);
element_to_focus.Focus(); //The textbox
Send(translate_string_to_key(text));
}
解决方案
ProcessInput
似乎只会引发适当的键相关事件,但不会实际修改文本。
如果您真的想模拟按键,则需要利用 p/invoke 并使用 SendInput
. 幸运的是,有人已经为我们完成了 p/invoke 工作并提供了他们的工作:
https ://archive.codeplex.com/?p=inputsimulator
它也可以作为 NuGet 包提供(我找到了两个版本):
https://www.nuget.org/packages/InputSimulator/
https://www.nuget.org/packages/InputSimulatorPlus/
推荐阅读
- javascript - 使用 NgMoudleFactoryLoader Angular 8 发布延迟加载模块
- git - 无法使用提交 ID 恢复丢失的文件
- arrays - 阅读此答案后,我了解其好处,但对功能感到困惑
- servlets - 在 Vaadin 14 应用程序中在哪里存储“UI”对象范围状态?
- python - 如何根据该变量的另一个值平等地对变量进行分类?
- javascript - 之前完全完成后的ajax执行功能
- reactjs - 反应钩子,提供上下文但不重新渲染状态变化的孩子
- jenkins-pipeline - Jenkins 管道 RejectedAccessException 错误
- opencv - 如何用opencv调用多个图像
- ruby - 如何使用 Ruby 从大文件中获取 CRC32 校验和?