按住键盘输入时犹豫? [英] Keyboard input hesitation when held down?

查看:138
本文介绍了按住键盘输入时犹豫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道为什么当你按住键盘键并尝试处理它有一些犹豫?我正在我的WinProc(...)中调用一个函数,将在按下键时在屏幕上移动图像(OpenGL)。我按下它,得到一个单一的响应,然后有大约.5秒的没有,然后它的行为正常(每个WinMain循环移动1像素)。

Does anyone know why there is some hesitation when you hold down a keyboard key and try to process it? I'm calling a function right in my WinProc(...) that will move an image on the screen (OpenGL) when a key is held down. I press it and get a single response, then there is about .5 seconds of nothing, then it behaves as normal (moves 1 pixel every WinMain loop).

m不知道Windows消息是否因某些功能而被延迟,因为我需要禁用

I'm wondering if the Windows messages are being delayed somehow because of some feature I need to disable???

这是我的代码:

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int nshowcmd)
{
    bool quit = false;
    MSG msg;

    createWindow(hinstance, SCRW, SCRH, SCRD, WINDOWED);

    // Main loop
    while (!quit)
    {		
    	if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
    	{
    		if (msg.message == WM_QUIT)
    			quit = true;

    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}

        renderFrame();        // processes graphics
    	SwapBuffers(hdc);
    }
    return msg.lParam;
}

和WinProc(有更多的案例,但同样的事情...):

and WinProc (there were more cases but same thing...):

    LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
        switch(msg)
        {
        	case WM_KEYDOWN:
        		switch ( wparam )
        		{
        			case VK_RIGHT:
        				key_RIGHT();
        				return 0;
        		}
        		return 0;
}
}

和key_RIGHT:

and key_RIGHT:

void key_RIGHT()
{
    MoveObjectRight1Pixel();
}


推荐答案

在Windows消息处理程序中处理键盘输入而不是处理键盘输入,而是保留一个数组的256位,指示键盘的当前状态。当您收到 WM_KEYDOWN WM_KEYUP 时,您更新相应密钥的位。然后,在你的主循环中,你检查键的当前状态和键的以前的状态(通过保留一个256位的第二个数组,你复制到每一帧)。

Instead of processing keyboard input in your Windows message handler, you could instead keep an array of 256 bits indicating the current state of the keyboard. When you receive a WM_KEYDOWN or WM_KEYUP, you update the bit of the corresponding key. Then, in your main loop, you check the current state of the key and the previous state of the key (by keeping a second array of 256 bits that you make a copy to every frame).

如果键在当前已关闭,但在上一帧中未关闭,则相应地移动对象。

If the key is currently down but was not down in the previous frame, then you move your object accordingly.

另一种方法是使用< a href =http://msdn.microsoft.com/en-us/library/ms646293%28VS.85%29.aspx =nofollow> GetAsyncKeyState() 函数。

Another alternative is to use the GetAsyncKeyState() function.

这篇关于按住键盘输入时犹豫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆