SendInput: Keys won't hold down

I've made successful progress since my last SendInput related thread but now I have a new issue: I'd like it to simulate a key being held down, but it's not working as explained in Microsoft's API.

As far as I can tell, the correct events are being sent here - a keycode is being sent with no flags when setState(true) is called (and the key wasn't previously pressed), and the same keycode is later sent with KEYEVENTF_KEYUP when the opposite occurs. I would expect this to simulate a key being held down, but it doesn't happen; it registers a single stroke and nothing more.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Key
{
    public:
        void setState(bool state);
        Key(WORD code);
    protected:
        bool keyState;
        WORD keyCode;
};

void Key::setState(bool state)
{
    if(keyState != state)
    {
        keyState = state;
        INPUT inputState;
        ZeroMemory(&inputState, sizeof(INPUT));
        inputState.type = INPUT_KEYBOARD;
        inputState.ki.wVk = keyCode;
        inputState.ki.dwFlags = state==true ? 0 : KEYEVENTF_KEYUP;
        SendInput(1, &inputState, sizeof(INPUT));
    }
}

Key::Key(WORD code)
{
    keyState = false;
    keyCode = code;
}


My code's logic follows that of examples I've seen that are purported to work properly. How can I fix this?
Topic archived. No new replies allowed.