Stupid .NET Trick #6

6:45 pm programming

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.

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.