Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WM_KEYDOWN repeat count?

Tags:

winapi

The MSDN documentation says that the LParam first 15 bits is used for repeat count , but it says that it is not accumulative

Now unless i am missing something here, why does it call it a repeat count but says it is not accumulative?

This is an oxymoron statement? It says it does but it doesn't? Or am i missing something here?

I Actually tested it and masked it with bitwise operator to extract those first 15 bits with LParam&0xFFFFand no matter how much i hold down the key , this value remains as 1

Unless i am doing something wrong or missing something, i dont know what is the point of this counter which doesnt count? Or am i misunderstanding something and doing this the wrong way and there is something that needs to be done to use this

It would be much more effective and convenient to have this counter so that i dont have to run all this other code to count the repeat count for the keys pressed and held , so is it possible to be done using those first 15 bits? maybe increment those first 15 bits?

like image 437
sporingGT Avatar asked Jul 04 '17 06:07

sporingGT


1 Answers

Lets start at the documentation:

The repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key.

This part is relatively straight forward. The repeat count field is the number of 'presses' the key had.

If the keystroke is held long enough, multiple messages are sent.

Multiple messages can be sent, depending on your message loop. Windows will keep sending you messages as long as the key is down, so you can keep processing repeats.

However, the repeat count is not cumulative.

The repeat count doesn't carry over between messages. In other words, each message represents the number of repeats since the last time you processed a WM_KEYDOWN message.

The reason you never see a repeat count above 1 is that you're processing window messages too quickly. You can see higher numbers by putting a delay into your WM_KEYDOWN message handler to allow more repeats to queue into the next message. (In C# here because there's less boilerplate code, but you should be able to translate it to whatever language you use.)

private const int WM_KEYDOWN = 0x0100;
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_KEYDOWN)
    {
        System.Threading.Thread.Sleep(1000);
        this.Text = $"Keydown Count: {m.LParam.ToInt32() & 0xFF}";
    }
    base.WndProc(ref m);
}

Running this code, I see repeat counts near 20-30.

If you need the total number of repeats, you will need to keep a running tally from the first WM_KEYDOWN until the WM_KEYUP. The design is so that you can process the events as they come in. (Picture a text box: responsiveness demands that you process the keys as they come in, rather than wait till the key is released.)

like image 61
theB Avatar answered Oct 03 '22 14:10

theB