如何从的SendKeys目前NET的形式/应用程序F12 [英] How to SendKeys F12 from current .NET form/application

查看:125
本文介绍了如何从的SendKeys目前NET的形式/应用程序F12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty的确认以下按钮激活的形式code应该提出一个Control-F12在我的C#应用​​程序:

I am pretty sure the following button-activated form code should raise a Control-F12 in my C# application:

的SendKeys(^ {F12});

SendKeys("^{F12}");

但它似乎没有去到Windows外壳程序并激活监听其另一个程序。我的键盘不工作。好像是越来越截取某处,而不是送键的方式,实际模拟关键笔划上发送。任何帮助?

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?

推荐答案

的SendKeys无法发送活动的应用程序之外的密钥。

SendKeys is not capable of sending keys outside of the active application.

要实实在在地模拟按键系统范围,你需要的P / Invoke或者 keybd_event SendInput user32.dll中。 (根据MSDN SendInput 是正确的方式,但 keybd_event 作品和更简单的P / Invoke。)

To really and truly simulate a keystroke systemwide, you need to P/Invoke either keybd_event or SendInput out of user32.dll. (According to MSDN SendInput is the "correct" way but keybd_event works and is simpler to P/Invoke.)

为例(我的认为的这些关键codeS是正确的......首先在每一对的 VK _ code ,第二个是成败的键盘扫描code ......2是 KEYEVENTF_KEYUP

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" is KEYEVENTF_KEYUP)

[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan,
    int dwFlags, int dwExtraInfo);

...

keybd_event(0xa2, 0x1d, 0, 0); // Press Left CTRL
keybd_event(0x7b, 0x58, 0, 0); // Press F12
keybd_event(0x7b, 0xd8, 2, 0); // Release F12
keybd_event(0xa2, 0x9d, 2, 0); // Release Left CTRL

另一种方法是激活你发送使用的SendKeys之前的应用程序。要做到这一点,你需要再次使用P / Invoke来寻找应用程序的窗口和焦点了。

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.

这篇关于如何从的SendKeys目前NET的形式/应用程序F12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆