Longish delay between two consecutive KeyDown events

自古美人都是妖i 提交于 2019-12-23 20:32:55

问题


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:


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.



来源:https://stackoverflow.com/questions/2340159/longish-delay-between-two-consecutive-keydown-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!