Stupid .NET Trick #6
September 26, 2007 6:45 pm programmingAs far as I can tell, you can’t handle key-repeating (ie: holding down a key for more than a brief period of time) in .NET without doing it manually through an obscene combination of KeyDown, KeyUp, and Timer. Google certainly doesn’t know how.
Update:After reading this blog I figured it out. There’s two gotchas involved:
- Multiple KeyDown (and presumably KeyUp and KeyPress) events are in fact fired when you hold the key down, even though this isn’t mentioned in the documentation.
- KeyDown isn’t fired at all for the arrow keys (even though KeyUp appears to be, at least in the code I had at one point) unless you convert the arrow key into an “Input Key”, via either PreviewKeyDown or IsInputKey. Since I was concerned with the arrow keys I happened to not realize the above point in testing.
Though it works, and in retrospect is quite easy to implement, it’s obscure enough that I’m not de-classifying this as a Stupid Trick.
