Solved

keyboard buffering

Posted on 2002-05-08
3
329 Views
Last Modified: 2010-04-07
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:

case WM_KEYDOWN:
..if (!keyBlock)
....poManager->ReceiveKeyPress(wParam);
....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)

0
Comment
Question by:kushcu
3 Comments
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 6999776
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.
0
 
LVL 2

Accepted Solution

by:
kamarey earned 71 total points
ID: 7001552

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;

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

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

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

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

void Someclass::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
     if (nKeysPressed)
        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])
        dosometing;

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

    etc
}
0
 

Author Comment

by:kushcu
ID: 7003488
I guess this stuff uses MFC, but anyway, the idea should solve the problem.
thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now