如何在 c++ 中将快捷方式发送到另一个应用程序在微软视窗中?

发布于 2024-09-14 18:39:17 字数 151 浏览 1 评论 0原文

我的问题是如何将快捷方式从 C++ 发送到另一个应用程序。比方说,记事本已打开,我想向他们发送快捷键(如 CTRL+P),或更复杂的快捷键(如 CTRL+SHIFT+HOME 或 ALT+F4),我发现许多很好的教程解释了如何发送一键像“A”或“ALT”,但我没有找到如何将它们一起发送。

My question is how to send shortcut from c++ to another application. Let's say for example that the notepad is open, and I want send to them shortcut like CTRL+P, or more complexive shortcut like CTRL+SHIFT+HOME, or ALT+F4, I find many good tutorials that explained how to send one key like 'A' or 'ALT', but I did not find how to send it together.

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

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

发布评论

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

评论(3

还给你自由 2024-09-21 18:39:17

您可以使用 SendInput()keybd_event() 将按键发送到当前聚焦的窗口。 SendInput() 可能是更好的选择,因为它不会将键盘事件与实际键盘的事件分散。

// send CTRL+SHIFT+HOME
INPUT input[6];
memset(input, 0, sizeof(INPUT)*6);
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = VK_CONTROL;
input[1].type = INPUT_KEYBOARD;
input[1].ki.wVk = VK_SHIFT;
input[2].type = INPUT_KEYBOARD;
input[2].ki.wVk = VK_HOME;
input[3].type = INPUT_KEYBOARD;
input[3].ki.wVk = VK_HOME;
input[3].ki.dwFlags = KEYEVENTF_KEYUP;
input[4].type = INPUT_KEYBOARD;
input[4].ki.wVk = VK_SHIFT;
input[4].ki.dwFlags = KEYEVENTF_KEYUP;
input[5].type = INPUT_KEYBOARD;
input[5].ki.wVk = VK_CONTROL;
input[5].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(6, input, sizeof(INPUT));

You can use SendInput() or keybd_event() to send keys to the currently focused window. SendInput() might be the better choice as it would not intersperse the keyboard events with those form the actual keyboard.

// send CTRL+SHIFT+HOME
INPUT input[6];
memset(input, 0, sizeof(INPUT)*6);
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = VK_CONTROL;
input[1].type = INPUT_KEYBOARD;
input[1].ki.wVk = VK_SHIFT;
input[2].type = INPUT_KEYBOARD;
input[2].ki.wVk = VK_HOME;
input[3].type = INPUT_KEYBOARD;
input[3].ki.wVk = VK_HOME;
input[3].ki.dwFlags = KEYEVENTF_KEYUP;
input[4].type = INPUT_KEYBOARD;
input[4].ki.wVk = VK_SHIFT;
input[4].ki.dwFlags = KEYEVENTF_KEYUP;
input[5].type = INPUT_KEYBOARD;
input[5].ki.wVk = VK_CONTROL;
input[5].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(6, input, sizeof(INPUT));
べ映画 2024-09-21 18:39:17

您可以发送多个 WM_KEYDOWN,然后发送多个 WM_KEYUP,这样您就可以尝试发送 DOWN ctrl、shift、home、UP home、shift、ctrl - 这应该可以解决问题。

You can send a number of WM_KEYDOWNs followed by a number of WM_KEYUPs so you could try sending DOWN ctrl, shift, home, UP home, shift, ctrl - that should do the trick.

苦妄 2024-09-21 18:39:17

这种“组合键”在 Microsoft Windows 中通常称为键盘加速器。 MSDN 介绍页面

This kind of "key combinations" is usually called Keyborad Accelerators in Microsoft Windows. The MSDN intro page

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