Search code examples
c++windowswinapiinputsendinput

Activate windows "Alt Tab" switcher with SendInput


I need to show the windows switcher with SendInput. Another question I asked explains the reason of doing this. Shortly speaking, when I am holding Alt Tab to switch to other apps, my app may fire a key stroke using SendInput, which will interrupt current switcher, and this is why I need to refire a Alt Tab. Currently I am working on posting another tab key stroke (I am still holding alt when switching) or the entirely alt down + tab down & up. But with alt holding, a single tab stroke sent by SendInput will not trigger the switcher. And the entire combined key does not work neither. Here's some test code.

#include <windows.h>
#include <WinUser.h>
#include <iostream>


int main(void) {
    Sleep(1000 * 3);
    INPUT tabinput[2];
    tabinput[0].type = INPUT_KEYBOARD;
    tabinput[0].ki = {0x09, 0}; // KEY_TAB = 0x09
    tabinput[1].type = INPUT_KEYBOARD;
    tabinput[1].ki = {0x09, 0, KEYEVENTF_KEYUP};
    SendInput(2, tabinput, sizeof(INPUT));
    getchar();
}

I'm trying to fire a tab key stroke delayed 3s. I'am holding the alt key. This doesn't work. But the tab key is triggered, because When I run this code and switch to a text editor or something, there will be a tab event. My system is win8.1 64bit.


Solution

  • Windows 8 is blocking you.

    In the Windows 8 security model, apps don’t have the privileges required to be a UI automation client. But you can write a desktop app that acts as an automation client with your app as the target. To do this, your desktop automation client app needs to be built with UIAccess permissions.

    Change the manifest to UIAccess="true" and require administrator priviledge, created a certificate, sign the application with that certificate, put it in a location under Program Files, and run it. As explained here

    https://msdn.microsoft.com/en-us/library/windows/desktop/dd979761.aspx?f=255&MSPPError=-2147217396

    and here

    https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/4b6dbc43-a026-4957-9178-91d2001e2d0d/windows-8-block-alttab-simulation#291eb5b4-f6d2-49b6-83db-658bd832f2c9

    plus this

    https://msdn.microsoft.com/en-us/library/ms742884.aspx?f=255&MSPPError=-2147217396

    and this

    https://translate.google.com/translate?sl=it&tl=en&js=y&prev=_t&hl=en&ie=UTF-8&u=http%3A%2F%2Fblogs.msdn.com%2Fb%2Fitasupport%2Farchive%2F2009%2F09%2F16%2Fsendsas-step-by-step.aspx&edit-text=&act=url