keyboard buffering

I'm currently working on a simple 2D game, and almost everything's complete expect for one disturbing problem. I decided to use keyboard for controlling the human ship:

..if (!keyBlock)
....keyBlock = true;
..return 0;

what the poManager does is to simply call another class' member function to do keyboard processing. (a set of switch-case statements that test which key has been pressed. -like UP, DOWN arrows, etc..)
A timer assigns the false value to keyBlock every 50ms.

The problem is when I'm controlling the ship, say I press the left arrow key, and hold it down. And then, just as every computer gamer does, I wish to go diagonally (left and up), and press the up arrow key. Unfortunately no. I keep going only left.

Is there any clean way to do this.
(I don't use directx, native win32 api)

Who is Participating?
kamareyConnect With a Mentor Commented:

class Someclass : public Baseclass
    // 128 - number of key;
    // 2 - two arrays, first column contain values
    // 'simple' keys, second column contain values
    // of extended keys(left, del, etc)
    BOOL key[128][2];
    DWORD nKeysPressed;

        nKeysPressed = 0;
        FillMemory(key, 128 * 2 * sizeof(BOOL), 0);

void Someclass::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    if (!key[nChar][(nFlags & 256) == 256])

        key[nChar][(nFlags & 256) == 256] = TRUE;

    Baseclass::OnKeyDown(nChar, nRepCnt, nFlags);

void Someclass::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
     if (nKeysPressed)

    key[nChar][(nFlags & 256) == 256] = FALSE;
    Baseclass::OnKeyUp(nChar, nRepCnt, nFlags);

void Someclass::ShipMove()
    // Test left(VK_LEFT) extended(TRUE) key
    if (key[VK_LEFT][TRUE])

    // Test key a(VK_A) not extended(FALSE)
    if (key[VK_A][FALSE])

Jan LouwerensSoftware EngineerCommented:
the 'keyblock' variable just specifies that a key was (and/or still is) pressed. Any other keypresses are lost or ignored.
Perhaps you should keep a list of boolean 'keyblock' variables, one for each key, and then test only the keyblock variable associated with that key in the WM_KEYDOWN message.
kushcuAuthor Commented:
I guess this stuff uses MFC, but anyway, the idea should solve the problem.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.