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?