SendInput on c++ doesn't take Ctrl and Shift in account

Viewed 1762

I'm trying to send a CTRL+SHIFT+END key stroke to a control, in a C++ application written with Embarcadero. I'm using the code below:

tagINPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;

// Appui sur Ctrl
ip.ki.wVk = VK_CONTROL;
ip.ki.dwFlags = 0;
SendInput(1, &ip, sizeof(INPUT));

// Appui sur Shift
ip.ki.wVk = VK_SHIFT;
ip.ki.dwFlags = 0;
SendInput(1, &ip, sizeof(INPUT));

// Appui sur End
ip.ki.wVk = VK_END;
ip.ki.dwFlags = 0;
SendInput(1, &ip, sizeof(INPUT));

Application->ProcessMessages();

// Relacher End
ip.ki.wVk = VK_END;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));

// Relacher Shift
ip.ki.wVk = VK_SHIFT;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));

// Relacher Ctrl
ip.ki.wVk = VK_CONTROL;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));

However, it appears to be working like if there was no action on the CTRL and SHIFT keys.

What am I missing ?

2 Answers

The problem is, that the VK_END key is an extended key. So you should declare this in the flags:

eip.ki.dwFlags = 0;
ip.ki.wVk = VK_END;
ip.ki.dwFlags = 0;
ip.ki.dwFlags = ip.ki.dwFlags | 1;
SendInput(1, &ip, sizeof(INPUT));

Application->ProcessMessages();

// Relacher End
ip.ki.wVk = VK_END;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
ip.ki.dwFlags = ip.ki.dwFlags | 1;
SendInput(1, &ip, sizeof(INPUT));

Note that I used 1 for setting the flag as I don't know if you have a constant defined in your C++, but assume there is a constant someplace.

Just add KEYEVENTF_EXTENDEDKEY flag to dwFlags

input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;

Unfortunately, Win32 doesn't provide any way to determine if a key is extended. Here I just used a vector

    extended_keys << VK_INSERT;
    extended_keys << VK_DELETE;
    extended_keys << VK_HOME;
    extended_keys << VK_END;
    extended_keys << VK_PRIOR;
    extended_keys << VK_NEXT;
    extended_keys << VK_LEFT;
    extended_keys << VK_UP;
    extended_keys << VK_DOWN;
    extended_keys << VK_RIGHT;

and checked if it's inside the list add the flag

Related