Stupid .NET Trick #6

September 26, 2007 by Craig

As 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:

  1. 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.
  2. 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.


No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment