When I was looking on how to handle key inputs in a game loop I came across the following pattern: All native key events (up or down) get queued in an event queue that is later processed. The outcome is an array, that says for each key if the key is down or up. Pretty straight forward. I then took a look in the DOOM 3 BFG source code and it seems, they do exactly the same:
DOOM-3-BFG/neo/sys/win32/win_wndproc.cpp
LONG WINAPI MainWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
int key;
switch( uMsg ) {
// ...
case WM_KEYDOWN:
// ...
Sys_QueEvent( SE_KEY, key, true, 0, NULL, 0 );
break;
case WM_SYSKEYUP:
case WM_KEYUP:
// ...
Sys_QueEvent( SE_KEY, key, false, 0, NULL, 0 );
break;
// ...
}
//...
}
DOOM-3-BFG/neo/framework/EventLoop.cpp
void idEventLoop::ProcessEvent( sysEvent_t ev ) {
// track key up / down states
if ( ev.evType == SE_KEY ) {
idKeyInput::PreliminaryKeyEvent( ev.evValue, ( ev.evValue2 != 0 ) );
}
// ...
}
DOOM-3-BFG/neo/framework/KeyInput.cpp
void idKeyInput::PreliminaryKeyEvent( int keynum, bool down ) {
keys[keynum].down = down;
}
I then started asking myself, what happens if a frame might take longer and the key down and up events happen in the same frame? Because that actually means that these events compete each other e.g. cancel each other out. How can this situation be handled or is this scenario uncommon?