如何从当前 .NET 表单/应用程序发送按键 F12
我非常确定以下按钮激活的表单代码应该在我的 C# 应用程序中引发 Control-F12:
SendKeys("^{F12}");
但它似乎并没有继续进入 Windows shell 并激活另一个正在侦听它的程序。 我的键盘可以用。 看起来发送键在某处被拦截,并且没有以实际模拟击键的方式发送。 有什么帮助吗?
I am pretty sure the following button-activated form code should raise a Control-F12 in my C# application:
SendKeys("^{F12}");
But it does not appear to go on up to the windows shell and activate another program that is listening for it. My keyboard does work. It seems like the sendkeys is getting intercepted somewhere and not sent on in a way that actually simulates the key stroke. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SendKeys 无法在活动应用程序之外发送密钥。
要真正模拟系统范围内的击键,您需要从
user32.dll
中 P/Invokekeybd_event
或SendInput
。 (根据 MSDNSendInput
是“正确”的方式,但keybd_event
可以工作并且更易于 P/Invoke。)示例(我认为这些键代码是正确的...每对中的第一个是
VK_
代码,第二个是接通或断开键盘扫描代码...“2”是KEYEVENTF_KEYUP
)另一种方法是在使用 SendKeys 之前激活您要发送到的应用程序。 为此,您需要再次使用 P/Invoke 来查找应用程序的窗口并将其聚焦。
SendKeys is not capable of sending keys outside of the active application.
To really and truly simulate a keystroke systemwide, you need to P/Invoke either
keybd_event
orSendInput
out ofuser32.dll
. (According to MSDNSendInput
is the "correct" way butkeybd_event
works and is simpler to P/Invoke.)Example (I think these key codes are right... the first in each pair is the
VK_
code, and the second is the make or break keyboard scan code... the "2" isKEYEVENTF_KEYUP
)The alternative is to activate the application you're sending to before using SendKeys. To do this, you'd need to again use P/Invoke to find the application's window and focus it.