1

I'm making a small program to select the keyword where I've placed the caret on. I'm trying to send Shift + Ctrl + Right Arrow from my program to any active window to select the text.

The problem is the "Shift" key is not sent and the text is not selected.

How can I do it?

This is my code that I have tried:

// Generate Ctrl + Shift + RightArrow input
INPUT copyText[6];

// Set the press of the "Shift" key
copyText[0].ki.wVk = VK_SHIFT; 
copyText[0].ki.dwFlags = 0; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;

// Set the press of the "Ctrl" key
copyText[1].ki.wVk = VK_CONTROL;
copyText[1].ki.dwFlags = 0; // 0 for key press
copyText[1].type = INPUT_KEYBOARD;

// Set the press of the "C" key
copyText[2].ki.wVk = VK_RIGHT;
copyText[2].ki.dwFlags = 0;
copyText[2].type = INPUT_KEYBOARD;

// Set the Release of the "Shift" key
copyText[3].ki.wVk = VK_SHIFT;
copyText[3].ki.dwFlags = KEYEVENTF_KEYUP; 
copyText[3].type = INPUT_KEYBOARD;

// Set the Release of the "Ctrl" key
copyText[4].ki.wVk = VK_CONTROL;
copyText[4].ki.dwFlags = KEYEVENTF_KEYUP; 
copyText[4].type = INPUT_KEYBOARD;

// Set the Release of the "C" key
copyText[5].ki.wVk = VK_RIGHT;
copyText[5].ki.dwFlags = KEYEVENTF_KEYUP;
copyText[5].type = INPUT_KEYBOARD;

// Send key sequence to system
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • That VB6 tag is coming out of left field. You should either justify it in the question body or remove it. – user4581301 Apr 12 '22 at 20:47
  • 2
    This may or may not be related, but in general, you should always zero out structs before passing them into the Win32 API, otherwise it may misinterpret uninitialized fields as intended input and misbehavior. Case in point, you are not zeroing out the `wScan` and `time` fields of each `INPUT` element in the array. Simplest way to do this is to change the declaration of `copyText` to `INPUT copyText[6] = {};` or use `ZeroMemory(&copyText, sizeof(copyText));` after the declaration. – Remy Lebeau Apr 13 '22 at 01:49
  • Try UI Automation. SendInput is a hack (what if the user is also pressing Alt when you fake input?). – Anders Apr 13 '22 at 15:19
  • i handled these issues, but my problem now is The Shift key is not sent – Peter Boshra Apr 13 '22 at 16:41
  • Here is the similar thread, I suggest you could refer to : https://stackoverflow.com/questions/44924962/sendinput-on-c-doesnt-take-ctrl-and-shift-in-account – Jeaninez - MSFT Apr 15 '22 at 08:21

0 Answers0