Windows Develop Bookmark and Share   
 index > Windows Forms General > Keyboard repeat count in C#
 

Keyboard repeat count in C#

Okay, I must be missing something. I know full well how Win32 will pass the repeat count of a key that has been pressed, but what about in C# ?

I am handling KeyDown events for a control, but I see no repeat count info in the "KeyEventArgs" object. What am I missing, or is there a way once inside the event handler to see if another event is pending?

I have to do some heavy processing in the key handler, but I can speed things up if I know how many times the key has repeated.

Thanks.
AZDean  Thursday, August 21, 2008 9:22 PM
Oddly, even the WM_KEYDOWN and WM_CHAR message never give a value other than 1 for the repeat count. You can certainly fix it with your own event handler:

private Keys mLastKey;
private int mRepeatCount = 1;

protected override void OnKeyDown(KeyEventArgs e) {
if (e.KeyCode == mLastKey) ++mRepeatCount;
else {
mRepeatCount = 1;
mLastKey = e.KeyCode;
}
base.OnKeyDown(e);
}

nobugz  Saturday, August 23, 2008 1:12 PM

To do this properly, one also needs to keep track of the time of the last key. Once enough time has passed, the key should not count as a repeat even if it is equal to mLastKey.

Better yet, as soon as the key is released (KeyUp), mLastKey should be cleared. I covered this recently in an answer here: http://forums.msdn.microsoft.com/en-US/vblanguage/thread/7d83f7d9-ae4e-4a04-8233-1a67284c396a

BinaryCoder  Saturday, August 23, 2008 5:37 PM

There is no repeat count because KeyDown and KeyUp only occur once for the activation and release of the key, respectively.

Compare this to the KeyPress event. The KeyPress event will be called once for each repetition.

> but I can speed things up if I know how many times the key has repeated.

You are probably going to need to keep track of what clock times keys have been recently pressed. You can then compare against this information when making decisions.

BinaryCoder  Thursday, August 21, 2008 10:25 PM
Oddly, even the WM_KEYDOWN and WM_CHAR message never give a value other than 1 for the repeat count. You can certainly fix it with your own event handler:

private Keys mLastKey;
private int mRepeatCount = 1;

protected override void OnKeyDown(KeyEventArgs e) {
if (e.KeyCode == mLastKey) ++mRepeatCount;
else {
mRepeatCount = 1;
mLastKey = e.KeyCode;
}
base.OnKeyDown(e);
}

nobugz  Saturday, August 23, 2008 1:12 PM
Thanks, looking at my old Win32 code, I would process the repeat count, but I would also check if another key was waiting in the message queue. So now that you say it, I vaguely remember that problem which is why I added the other check.

Anyway, I already had solved the problem the way you suggest, though it meant that I needed a separate thread that when it had time to look at what came in, could then process the number of key repeats that had occurred while it was busy.

Dean
AZDean  Saturday, August 23, 2008 1:41 PM

To do this properly, one also needs to keep track of the time of the last key. Once enough time has passed, the key should not count as a repeat even if it is equal to mLastKey.

Better yet, as soon as the key is released (KeyUp), mLastKey should be cleared. I covered this recently in an answer here: http://forums.msdn.microsoft.com/en-US/vblanguage/thread/7d83f7d9-ae4e-4a04-8233-1a67284c396a

BinaryCoder  Saturday, August 23, 2008 5:37 PM

You can use google to search for other answers

Custom Search

More Threads

• Threading question
• Help me - Value
• Controlling Tab appearance
• treeview nodes with images and without
• PDF in a web browser control
• Application.ThreadException and AppDomain.CurrentDomain.UnhandledException events
• cannot show a popup form in xp, but work in win2k
• how to replace exact word from string
• How to retrieve the LV_ITEM's info by SendMessage
• Deleting non focused controls at runtime .