0

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.

Community
  • 1
  • 1
Joey.Z
  • 4,492
  • 4
  • 38
  • 63
  • Stop faking input and all your problems go away – David Heffernan Jul 25 '15 at 20:02
  • @DavidHeffernan What does this mean, this is a concrete problem under a common situation. Yes, you may say that it's not reasonable or elegant to fake input, can you give a better suggestion to solve my problem? That is to reset the state of caps without faking input so not to interrupt current key OR to refire the alt tab after interruption by faking input. – Joey.Z Jul 26 '15 at 02:53
  • For those who downvote this question and the other one. Give your argument plz. – Joey.Z Jul 26 '15 at 02:55
  • Solve your problem by letting the user manage the caps lock state. Remove all the input faking and all problems are solved. – David Heffernan Jul 26 '15 at 06:53
  • @DavidHeffernan If I do nothing I will never encounter those problems, sounds reasonable. If you are interested, though I strongly doubt that according to your suggestions, plz see the solutions given by@Software_Designer and my supplements. Have a nice day! – Joey.Z Jul 26 '15 at 09:49
  • That's a terrible thing to do. Your judgement is way off. Users already know how to use the caps lock key. – David Heffernan Jul 26 '15 at 11:07
  • @DavidHeffernan OK, let me explain in more detail. My app use `caps+M` to open up something like console, and user can modify the script code outside the app and turn to the console to test. If you are a vim user you will understand that with caps on, it will mess up things like moving cursor. And if you use other text editor, that is not adorable too. Does this make any sense to you? – Joey.Z Jul 26 '15 at 11:16
  • So I want to deactive caps when I switch from my app. – Joey.Z Jul 26 '15 at 11:23
  • You seem happy with your solution. That's fine. – David Heffernan Jul 26 '15 at 11:29

2 Answers2

3

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

Software_Designer
  • 8,490
  • 3
  • 24
  • 28
  • So, I test this solution on my laptop (win8.1 64bit). Finally, it works! I got the Alt-Tab switcher showed up. Following is some supplements that may help to solve a similar problem and the other problems that I encountered during the test. https://msdn.microsoft.com/en-us/library/ff699202.aspx https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/350ceab8-436b-4ef1-8512-3fee4b470c0a/problem-with-manifest-and-uiaccess-set-to-true http://stackoverflow.com/questions/9506671/why-do-i-keep-getting-a-failed-when-trying-to-make-a-cer-for-testing And this is the constructive answer. – Joey.Z Jul 26 '15 at 09:43
  • Ouch!! Sledgehammer cracking the nut, and what about the security implications. – David Heffernan Jul 26 '15 at 11:06
  • BTW: Windows 10 already fixed this without any need of these complicated procedure. I've test under windows 10 Pro 64-bit. – Joey.Z Aug 19 '15 at 03:32
0

Arrays are 0-indexed, but you are populating the array using 1-based indexes. So you are not populating the first array element, and are trashing memory after the last array element. Use this instead:

int main(void) {
    Sleep(1000 * 3);
    INPUT tabinput[2];
    tabinput[0].type = INPUT_KEYBOARD;
    tabinput[0].ki = {VK_TAB, 0};
    tabinput[1].type = INPUT_KEYBOARD;
    tabinput[1].ki = {VK_TAB, 0, KEYEVENTF_KEYUP};
    SendInput(2, tabinput, sizeof(INPUT));
    getchar();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Sorry for my mistake, this is a test code, it does wrong as you pointed out. But even if I fix the mistake, this code can't do what I wanted to. Have you test this code on your machine (run it as you holding alt key)? And if the windows switcher showed up or not? – Joey.Z Jul 25 '15 at 16:45