如何将 Ctrl/Shift/Alt + 组合键发送到应用程序窗口? (通过发送消息)

2024-01-11

I can successfully send any single key message to an application, but I don't know how to send combinations of keys (like Ctrl+F12, Shift+F1, Ctrl+R, etc..)

尝试这样做:

SendMessage(handle, WM_KEYDOWN, Keys.Control, 0);
SendMessage(handle, WM_KEYDOWN, Keys.F12, 0);
SendMessage(handle, WM_KEYUP, Keys.F12, 0);
SendMessage(handle, WM_KEYUP, Keys.Control, 0);

but this does not seems to work (application acts like only F12 is pressed, not Ctrl+F12).

有什么想法可以让这项工作发挥作用吗?


You would probably find that using SendInput (documentation here) http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx works a lot better. You will need to P/Invoke it from C#, example here http://www.pinvoke.net/default.aspx/user32.sendinput. You can provide arrays of data with keys down and up and properly set the other message parameters, for example whether left or right Ctrl/Shift/Alt were pressed.

You can also use the SendKeys class (documentation here http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx). This allows you to specify keys by name, e.g., {^F12} for Ctrl+F12.

Edit:OP 现在说他需要将输入发送到最小化的应用程序而不激活它们。这是不可能可靠地做到的any way,甚至包括专门的硬件。我从事自动化工作。这是不可能的。 OP需要使用FindWindow/SetForegroundWindow打开目标应用程序,然后他可以切换回他的应用程序。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 Ctrl/Shift/Alt + 组合键发送到应用程序窗口? (通过发送消息) 的相关文章

随机推荐