views:

123

answers:

1

I'm kind of writing a little game engine -- purely to understand how these work from the inside. I currently don't want to mess with OpenGL or DirectX, so I stick to drawing on a control with GDI+ and all that WinForms stuff.

Obviously, I need to handle input. More specifically, keyboard events. This, however, poses a problem:

protected override void OnKeyDown(KeyEventArgs e)
{
    Trace.WriteLine(string.Format("KD {0:hh:MM:ss.fff} {1}", 
        DateTime.Now, e.KeyCode));
}

This code (even with the shortest repeat delay set in the Keyboard applet in the Control Panel) yeilds the following:

KD 10:02:18.318 Right
KD 10:02:18.570 Right
KD 10:02:18.598 Right
KD 10:02:18.639 Right
KD 10:02:18.667 Right
KD 10:02:18.701 Right

As you can see, there's a 0.25 sec. delay between first two events. This results in sluggish movements of objects on screen, obviously: it first moves slightly to the right, then pauses for a noticeable moment, and then continues on.

How do I resolve that issue? Can this be done in pure WinForms or should I go DirectInput (or whatever is the kosher way nowadays?) route?

+1  A: 

Using Windows messages isn't the best way to go for input interaction.
I know nothing about WinForms, but I assume on key events use messages.

I've used DirectInput (v7) many years ago, and it was really fast.
Perhaps GetKeyboardState or GetAsyncKeyState in a game loop are good alternatives.

Nick D
Whoa! That's _way_ faster!
Anton Gogolev
@Anton, which one? GetKeyboardState or GetAsyncKeyState?
Nick D
`GetKeyboardState` turned out to be fast enough, so I never tried `GetAsyncKeyState`.
Anton Gogolev
@Anton, ok :) DirectInput gives you "direct" access to keyboard's buffer so use it in case you need very accurate input, or in case you want to handle gamepads/joysticks and take advantage of their extra functionalities.
Nick D