0

I am working on one project where I am using INPUT structure and SendInput() to send Input command to another window using Hook.

I am using following code to get the handle of the last visited Application Window.

            INPUT input;
        input.type = INPUT_KEYBOARD;
        input.ki.time = 0;
        input.ki.dwExtraInfo = 0;
        input.ki.wScan = 0; 
        input.ki.dwFlags = 0;
        // Trial

        // ALT Key Down 
        input.ki.wVk = VK_MENU;
        SendInput( 1, &input, sizeof( INPUT ) );

        // Tab Key Down
        input.ki.wVk = VK_TAB;
        input.ki.dwFlags = 0;
        SendInput( 1, &input, sizeof( INPUT ) );

        // Tab Up
        input.ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput( 1, &input, sizeof( INPUT ) );

        // ALT Key  Up
        input.ki.wVk = VK_MENU;
        SendInput( 1, &input, sizeof( INPUT ) );

        // ALT Up
        input.ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput( 1, &input, sizeof( INPUT ) );

        // Give Sleep Time
        Sleep(500);

        // ALT Key Down 
        input.ki.wVk = VK_MENU;
        SendInput( 1, &input, sizeof( INPUT ) );

        // Tab Key Down
        input.ki.wVk = VK_TAB;
        input.ki.dwFlags = 0;
        SendInput( 1, &input, sizeof( INPUT ) );

        // Tab Up
        input.ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput( 1, &input, sizeof( INPUT ) );


        // ALT Key  Up
        input.ki.wVk = VK_MENU;
        SendInput( 1, &input, sizeof( INPUT ) );

        // ALT Key Up
        input.ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput( 1, &input, sizeof( INPUT ) );
// Get Last Window Handle from the Hook Dll

and Then Send Paste Command:

                // Send Paste Command To Previous Window
                // Ctrl Down
                input.ki.wVk = VK_CONTROL;
                input.ki.dwFlags = 0;
                ::ShowWindow(mainHwnd, SW_SHOW);
                SendInput( 1, &input, sizeof( INPUT ) );

                // V Down
                input.ki.wVk = 0x56;
                ::ShowWindow(mainHwnd, SW_SHOW);
                SendInput( 1, &input, sizeof( INPUT ) );

                // V Up
                input.ki.dwFlags = KEYEVENTF_KEYUP;
                ::ShowWindow(mainHwnd, SW_SHOW);
                SendInput( 1, &input, sizeof( INPUT ) );

                // Ctrl Up
                input.ki.wVk = VK_CONTROL;
                ::ShowWindow(mainHwnd, SW_SHOW);
                SendInput( 1, &input, sizeof( INPUT ) );

                // Tab Down 
                input.ki.wVk = VK_TAB;
                input.ki.dwFlags = 0;
                ::ShowWindow(mainHwnd, SW_SHOW);
                SendInput( 1, &input, sizeof( INPUT ) );

                // Tab Up
                input.ki.dwFlags = KEYEVENTF_KEYUP;
                ::ShowWindow(mainHwnd, SW_SHOW);
                SendInput( 1, &input, sizeof( INPUT ) );

Now the problem is I am able to work with this on Win 7, 8 and 8.1. but I am getting problem on XP that the first block of program is pressing extra TAB before sending CTRL-V Command.

Can Any one tell me Why this is happening? And How can I solve this? Any time of Suggestions will be appreciated. Thanks in advance.

A B
  • 1,461
  • 2
  • 19
  • 54
  • You might want to try putting in a delay between the various key presses and releases. A real keyboard wouldn't change state that quickly, and this could be confusing Windows and/or the relevant application(s). – Harry Johnston Feb 28 '14 at 02:16
  • @HarryJohnston I have tried with the delay of `5 sec` but the result of the operation is same on xp – A B Feb 28 '14 at 03:18
  • Did you try putting a delay after *every* call to SendInput? – Harry Johnston Feb 28 '14 at 03:22
  • @HarryJohnston yes I have tried just now but the result is same. – A B Feb 28 '14 at 03:41
  • If you build a console-mode test program, you can print out what part of the code you've reached. Make the delays long enough, and you'll be able to see at exactly what point in the code the extra TAB character is appearing. Hopefully that will give you a clue. Incidentally, the calls to `ShowWindow` look dodgy to me. Try removing them. – Harry Johnston Feb 28 '14 at 03:46

0 Answers0