windows - 在 C++ 中模拟 Alt Tab 键盘按下以启动 Fast Switch 窗口

标签 windows c++-cli keyboard-shortcuts keyboard-events alt-tab

我有一个需要在触摸屏设备上运行的项目的想法。这个想法是在屏幕上有一个按钮,按下它可以在打开的项目之间切换。那么 ALT + TAB 键盘快捷键究竟是如何工作的。 我知道 C++ 中的 SendKeys::Send() 事件可以模拟按键,但当我尝试发送 ALT + TAB 时它似乎对我不起作用。那么有没有一种方法可以让窗口通过 C++ 显示所有打开的程序(就像按下 ALT TAB 时一样)?

PS 该项目是Windows 应用程序! Windows 7 开始,但希望它可以在以后与更多 Windows 系统兼容。

最佳答案

自从您提到 SendKeys 以来假设使用 C++/CLI。 SendKeys 不能可靠地工作,因为它会释放按键,使 Alt-Tab 窗口消失。您想改用 SendInput() 并为 Alt 键发送 keydown,为 Tab 键发送 keydown + up。此代码运行良好:

#include <windows.h>
#pragma comment(lib, "user32.lib")

...
    System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
        INPUT input = {INPUT_KEYBOARD};
        input.ki.wVk = (WORD)Keys::Menu;
        UINT cnt = SendInput(1, &input, sizeof(input));
        input.ki.wVk = (WORD)Keys::Tab;
        if (cnt == 1) cnt = SendInput(1, &input, sizeof(input));
        input.ki.dwFlags = KEYEVENTF_KEYUP;
        if (cnt == 1) cnt = SendInput(1, &input, sizeof(input));
        if (cnt != 1) throw gcnew System::ComponentModel::Win32Exception;
    }

关于windows - 在 C++ 中模拟 Alt Tab 键盘按下以启动 Fast Switch 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8125270/

相关文章:

c# - "Property elements cannot be in the middle of an element' s 内容。”WPF 应用程序中的错误

c# - 线程同步和中止

intellij-idea - 删除 IntelliJ 中的周围方法

c++ - Windows 中 code::blocks (C++) 中父/子进程的大多数准系统示例

windows - 如何自动提交Plink "Access granted. Press Return to begin session"提示

c - 限制进程对管道的访问 (Windows)

c++ - 混合模式项目的内存泄漏检测 : managed, 非托管和 native

c# - 在 C# - C++/CLI - C++ Windows 窗体应用程序退出之前跟踪 - 并正确结束 - native 和托管线程

visual-studio-code - 如何在 VS Code 中使用键盘垂直移动选定的文本

c# - 将 wpf 窗口聚焦在快捷键上