2

I am trying to write an application for Windows. The application should mark text, where the curser is. I want to use the SendInput() function from the <Windows.h> header file. My code so far looks like the following:

#define WINVER 0x0500
#include <windows.h>
 
int main()
{
    INPUT shift;
    INPUT home;
 
    Sleep(5000);
 
    shift.type = INPUT_KEYBOARD;
    home.type = INPUT_KEYBOARD;

    shift.ki.wScan = 0;
    home.ki.wScan = 0;

    shift.ki.time = 0;
    home.ki.time = 0;

    shift.ki.dwExtraInfo = 0;
    home.ki.dwExtraInfo = 0;

    shift.ki.wVk = VK_SHIFT;
    home.ki.wVk = VK_HOME;

      /*  INPUT testarrow;
        testarrow.type = INPUT_KEYBOARD;
        testarrow.ki.wScan = 0;
        testarrow.ki.time = 0;
        testarrow.ki.dwExtraInfo = 0;
        testarrow.ki.wVk = VK_LEFT;

        Sleep(100);
        shift.ki.dwFlags = 0; // 0 for key press
        SendInput(1, &shift, sizeof(INPUT));

        Sleep(1000);
        testarrow.ki.dwFlags = 0;
        SendInput(1, &testarrow, sizeof(INPUT));
        Sleep(1000);
        testarrow.ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput(1, &testarrow, sizeof(INPUT));
        Sleep(1000);
        shift.ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput(1, &shift, sizeof(INPUT));
        return 0;*/

        Sleep(100);
        shift.ki.dwFlags = 0;
        SendInput(1, &shift, sizeof(INPUT));
        Sleep(100);
        home.ki.dwFlags = 0;
        SendInput(1, &home, sizeof(INPUT));
        Sleep(100);
        home.ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput(1, &home, sizeof(INPUT));
        shift.ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput(1, &shift, sizeof(INPUT));
}

The code basically virtually presses the shift key, then uses the home key, to jump to the beginning of the line then unpresses the home key and finally unpresses the shift key. I also tried using the left arrow key, as you can see in the commented section.

I also tried using VK_LSHIFT and VK_RSHIFT instead of VK_SHIFT, but neither of them worked. In all tests, the curser just jumped to the beginning of the the text line, without marking it. I tried using shift in the normal way: using it to write uppercase letters, and this works perfectly fine.

I am limited to use just the <Windows.h> header and the C programming language.

Any idea, what is not working correctly?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
The A
  • 188
  • 1
  • 9
  • There is some discussion in [SendInput() not equal to pressing key manually on keyboard](https://stackoverflow.com/questions/18647053/sendinput-not-equal-to-pressing-key-manually-on-keyboard-in-c). Although it's tagged C++ it could be useful. – Weather Vane Nov 10 '20 at 20:26
  • I tried using the Scancodes for the shift key. I used the first answer about them and eddited my code accordingly. In used the part from my question, which I marked as a comment for testing. The curser got just moved to the left. I used scancode 0x2A as the table says. Nothing got highlighted in the Text window (Windows Text editor, although it shouldn't matter). – The A Nov 10 '20 at 20:51
  • I used a letter to verify, that the shift key actually got pressed: The letter got printed uppercase, as expected. (still no highlighting) – The A Nov 10 '20 at 20:53

0 Answers0