• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

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)

1 Solution
Jan LouwerensCommented:
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.

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])

kushcuAuthor Commented:
I guess this stuff uses MFC, but anyway, the idea should solve the problem.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now