10

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?

sporingGT
  • 151
  • 1
  • 9
  • 1
    Its not cumulative in that it does not count sequential presses until the key is released. I'd expect that, If your message loop is processing messages slowly such that multiple keypresses register between loops, then GetMessage (or is it TranslateMessage?) will generate a single message with a >1 count. – Chris Becke Jul 04 '17 at 06:04

1 Answers1

9

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.)

theB
  • 6,450
  • 1
  • 28
  • 38
  • 1
    so basically , after that message gets processed the WND_PROCEDURE function finishes, the next message is processed in message queue which happens to be the same key in auto repeat as WM_KEYDOWN and it only shows those first 15 bits as only 1 , and it completes before that first 15 bits is incremented again and goes to process the next message and so on, this is correct? – sporingGT Jul 05 '17 at 04:17
  • 1
    Yep, you got it. – theB Jul 05 '17 at 11:47
  • 1
    thanks for that, yeah i was actually printing out the value of first 15 bits in a switch clause for WM_KEYDOWN in the WNDPROC funtion, so since the flow of the program is being controlled by that function pointer , i can see why it does what it does now – sporingGT Jul 05 '17 at 22:44
  • 1
    i want to ask u quickly, if each time i enter the wndprocedure function and increment some counter value(global) each time the WM_KEYDOWN message for that key is held , is that sufficient to capture the amount of repeat times of some key, or do i still need to add some system delay? – sporingGT Jul 05 '17 at 22:48