SendKeys.SendWait 在某些情况下不起作用

发布于 2024-09-01 16:18:48 字数 213 浏览 4 评论 0原文

我的目标是将键盘事件发送到外部应用程序。 在我的应用程序中,我启动了一个 C# 控制台应用程序,它将目标应用程序置于前端并使用 SendKeys.SendWait 发送键盘事件。我遇到了一个速率情况,该命令没有任何影响。

调试它时,它可以工作,但是当不在调试中运行它时,它会失败。 我认为这与调试时我的应用程序是活动应用程序这一事实有关。

My target is to send keyboard events to external application.
From my application, I'm launching a C# console application that bring the target application to the front and uses SendKeys.SendWait to send keyboards events. I ran into a rate case where the command doesn't have any affect.

When debugging it, it works but when running it not in debug it fails.
I think it as something to do with the fact that when debugging my application is the active application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无力看清 2024-09-08 16:18:48

您需要做一些工作,并且它会根据 Windows 版本而变化。有一个 MSDN 页面有很好的解释和示例:

http://msdn .microsoft.com/en-us/library/ms171548.aspx

You'll need to do a little work, and it changes depending on the version of Windows. There's an MSDN page that has a good explanation and an example:

http://msdn.microsoft.com/en-us/library/ms171548.aspx

橙幽之幻 2024-09-08 16:18:48

你可以试试这个,它对我有用。

var text = "Hello";
var target = Keyboard.FocusedElement;
var routedEvent = TextCompositionManager.TextInputEvent;
target.RaiseEvent( new TextCompositionEventArgs( InputManager.Current.PrimaryKeyboardDevice, 
new TextComposition(InputManager.Current, target, text)) { RoutedEvent = routedEvent });

https://stackoverflow.com/a/1646568

You can try this, it works for me.

var text = "Hello";
var target = Keyboard.FocusedElement;
var routedEvent = TextCompositionManager.TextInputEvent;
target.RaiseEvent( new TextCompositionEventArgs( InputManager.Current.PrimaryKeyboardDevice, 
new TextComposition(InputManager.Current, target, text)) { RoutedEvent = routedEvent });

https://stackoverflow.com/a/1646568

万劫不复 2024-09-08 16:18:48

也许您应该以管理员身份运行您的程序。至少,在某些情况下它对我有用。

Maybe you should run your program as an administrator. At least, it works for me in some cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文