Solved

keyboard buffering

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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 …
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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

10 Experts available now in Live!

Get 1:1 Help Now