Solved

keyboard buffering

Posted on 2002-05-08
3
332 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ckserve 7 187
Python and Network Datagrams 7 420
How are Tech stock earnings calculated? 2 72
Game w 3D galaxy - How should I do vertical distancing? 3 79
What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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