How Can I Send Selection and Copy Keys Using SendINput

Peter Boshra 66 Reputation points
2022-04-17T17:23:01.483+00:00

I Want To Send These Keys To Active Window To Copy The Text which i stand on it
Keys : Shift + Right Arrow
Then : Ctrl + Left Arrow
Then : Shift + Ctrl + Right Arrow
Then : Ctrl + C

// I Tried This Code But Shift Key not Released so, Copy Operation have not Done
// i wrote This Code also in one array in sent as one but same problem

// Wait until all modifiers will be unpressed (to avoid conflicts with the other shortcuts)
while (GetAsyncKeyState(VK_LWIN) || GetAsyncKeyState(VK_RWIN) || GetAsyncKeyState(VK_SHIFT) || GetAsyncKeyState(VK_MENU) || GetAsyncKeyState(VK_CONTROL)) {};

//// Generate Ctrl + C input
INPUT copyText[1];
ZeroMemory(&copyText, sizeof(copyText));


// Set the press of the "Right" key
copyText[0].ki.wVk = VK_RIGHT;
copyText[0].ki.dwFlags = 0; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

// Set the Release of the "Right" key
copyText[0].ki.wVk = VK_RIGHT;
copyText[0].ki.dwFlags = KEYEVENTF_KEYUP; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

// Set the press of the "Ctrl" key
copyText[0].ki.wVk = VK_CONTROL;
copyText[0].ki.dwFlags = 0; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

// Set the press of the "Left" key
copyText[0].ki.wVk = VK_LEFT;
copyText[0].ki.dwFlags = 0; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));


// Set the Release of the "Left" key
copyText[0].ki.wVk = VK_LEFT;
copyText[0].ki.dwFlags = KEYEVENTF_KEYUP; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));


// Set the Release of the "Ctrl" key
copyText[0].ki.wVk = VK_CONTROL;
copyText[0].ki.dwFlags = KEYEVENTF_KEYUP; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));


// Set the press of the "Ctrl" key
copyText[0].ki.wVk = VK_CONTROL;
copyText[0].ki.dwFlags = 0; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

//// Set the press of the "Shift" key
copyText[0].ki.wVk = VK_SHIFT;
copyText[0].ki.dwFlags = KEYEVENTF_EXTENDEDKEY | 0;
copyText[0].type = INPUT_KEYBOARD; // Type Of Input
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

// Set the press of the "Right" key
copyText[0].ki.wVk = VK_RIGHT;
copyText[0].ki.dwFlags = 0; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

// Set the Release of the "Right" key
copyText[0].ki.wVk = VK_RIGHT;
copyText[0].ki.dwFlags = KEYEVENTF_KEYUP; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

////// Set of Release the "Shift" Key
copyText[0].ki.wVk = VK_SHIFT;
copyText[0].ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

// Set the press of the "Ctrl" key
copyText[0].ki.wVk = VK_CONTROL;
copyText[0].ki.dwFlags = KEYEVENTF_KEYUP; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

// Set the press of the "Ctrl" key
copyText[0].ki.wVk = VK_CONTROL;
copyText[0].ki.dwFlags = 0; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

// Set the press of the "C" key
copyText[0].ki.wVk = 'C';
copyText[0].ki.dwFlags = 0; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

// Set the Release of the "C" key
copyText[0].ki.wVk = 'C';
copyText[0].ki.dwFlags = KEYEVENTF_KEYUP; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
ZeroMemory(&copyText, sizeof(copyText));

// Set the Release of the "Ctrl" key
copyText[0].ki.wVk = VK_CONTROL;
copyText[0].ki.dwFlags = KEYEVENTF_KEYUP; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
Developer technologies C++
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,681 Reputation points
    2022-04-17T18:59:06.967+00:00

    I don't understand your keys combinations :
    Shift + Right Arrow
    Then :
    Ctrl + Left Arrow

    as the second keys cancel the first ones...

    Otherwise, you can adapt this test which works to select and copy all text in Notepad ([Ctrl] + [a], then [Ctrl] + [c] ) =>

    193639-sendinput-test.gif

                HWND hWndDest = FindWindow(L"Notepad", NULL);  
                if (hWndDest)  
                {  
                    SwitchToThisWindow(hWndDest, TRUE);  
                    SendKey(VK_CONTROL, 100, 0, true, false);  
                    SendKey((WORD)LOBYTE(VkKeyScan('a')), 100, 0, true, true);  
                    SendKey(VK_CONTROL, 100, 0, false, true);  
    
                    SendKey(VK_CONTROL, 100, 0, true, false);  
                    SendKey((WORD)LOBYTE(VkKeyScan('c')), 100, 0, true, true);  
                    SendKey(VK_CONTROL, 100, 0, false, true);  
                }  
    

    Utility function :

    void SendKey(WORD wVk, int nSleep, BOOL bExtendedkey, BOOL bDown, BOOL bUp)  
    {  
        INPUT input;  
        if (bDown)  
        {  
            input.ki.wVk = wVk;  
            input.ki.wScan = (WORD)MapVirtualKey(wVk, 0);  
            input.ki.dwFlags = KEYEVENTF_SCANCODE;  
            if (bExtendedkey)  
                input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;  
            input.ki.time = 0;  
            input.ki.dwExtraInfo = GetMessageExtraInfo();  
            input.type = INPUT_KEYBOARD;  
            SendInput(1, &input, sizeof(INPUT));  
            Sleep(nSleep);  
        }  
      
        if (bUp)  
        {  
            input.ki.wVk = wVk;  
            input.ki.wScan = (WORD)MapVirtualKey(wVk, 0);  
            input.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;  
            if (bExtendedkey)  
                input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;  
            input.ki.time = 0;  
            input.ki.dwExtraInfo = GetMessageExtraInfo();  
            input.type = INPUT_KEYBOARD;  
            SendInput(1, &input, sizeof(INPUT));  
            Sleep(nSleep);  
        }  
    }  
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.