Don't use Thread.Sleep
at all!
Set the KeyPreview
of the form to true
and use the key events of the form.
This will allow you to get notified of key events without lag and without a loop freezing the UI.
See also this answer to Winforms : Intercepting Mouse Event on Main Form first, not on Controls.
You also might use a gaming API instead. Those allow you to react on mouse and key events very fast and not tied to controls. They also allow you to detect simultaneous key presses.
Since you are not doing this in an UI (according to your comment), I suggest a different approach:
In a game you are usually using a gaming loop that loops as fast as possible, without any Thread.Sleep
delay. At every loop you check the current state of the input (keyboard, mouse, joystick) and update the game state and display accordingly.
At every loop save the current time with DateTime.UtcNow
(faster than DateTime.Now
) and check the time elapsed since an action happened to know whether the required delay has been reached.
If DateTime
is not accurate enough, use the StopWatch
class. See: Precision and accuracy of DateTime.