两个连续的KeyDown事件之间的长时间延迟 [英] Longish delay between two consecutive KeyDown events
问题描述
我有点在写一个小游戏引擎-纯粹是为了从内部了解它们的工作原理.我目前不希望将OpenGL或DirectX弄乱,所以我坚持使用GDI +和所有WinForms控件绘制控件.
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
如您所见,这是0.25秒.前两个事件之间的延迟.显然,这会导致屏幕上的对象运动缓慢:它先向右微移,然后暂停一会儿,然后继续.
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.
我该如何解决该问题?可以在纯WinForms中完成此操作,还是我应该使用DirectInput(或如今的犹太洁食方式?)路由?
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?
推荐答案
使用Windows消息并不是进行输入交互的最佳方法.
我对WinForms一无所知,但我假设在关键事件上使用消息.
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.
很多年前,我已经使用DirectInput(v7),而且速度非常快.
也许 GetKeyboardState 或 GetAsyncKeyState 是不错的选择.
I've used DirectInput (v7) many years ago, and it was really fast.
Perhaps GetKeyboardState or GetAsyncKeyState in a game loop are good alternatives.
这篇关于两个连续的KeyDown事件之间的长时间延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!