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

问题描述 投票:0回答:2

我的目标是将键盘事件发送到外部应用程序。 在我的应用程序中,我正在启动一个 C# 控制台应用程序,它将目标应用程序带到最前面,并使用

SendKeys.SendWait
发送键盘事件。我遇到了命令没有任何影响的费率情况。

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

c# console-application sendkeys
2个回答
2
投票

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

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


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.