Skip to main content
GameDev.net gamedev.net
🔒 Locked

DirectInput question: Do I need buffered input?

Started by Alex Swinney Nov 22, 2004 at 8:35 AM 4 replies 1k views
Original Post
Alex Swinney
Alex Swinney
My current project just polls the keyboard and mouse for input, unbuffered, and I think this is giving me problems, or maybe there is a way around my problem, I'm not sure. See, when I hit a button, the keyboard is polled so fast that yes, the program can tell that the button has been hit, but it does so repeatedly, which is not exactly what I want. How do I make it so that I get what I want done, but not repeatedly, which is not what I want? Case in point, I have debug info being printed to the screen, if I press 'F1', it is supposed to go away (or I planned it that way), but instead it flickers on and off and on and off and on and off... Do I use buffered input? Or is there some method to use instead? Thanks!
I WISH SOMEONE WOULD FIX THE DAMNED LOGIN!
Pipo DeClown
Pipo DeClown
You have to *implement* locks. Yes you have to code it, here is how it works.
) If the key is pressed, lock it.
) Until the key is NOT-pressed, then unlock it.
This way, you have to release the key before it's usable again.

Tip: use a bool[256].
Alex Swinney
Alex Swinney
Quote:
Original post by Pipo DeClown
You have to *implement* locks. Yes you have to code it, here is how it works.
) If the key is pressed, lock it.
) Until the key is NOT-pressed, then unlock it.
This way, you have to release the key before it's usable again.

Tip: use a bool[256].


Ok, the way I see it is this:

bool keys[256];for(int i = 0;i<256;i++)    this->keys = false;if(KEYDOWN(this->buf,DIK_SPACE)&& !keys[DIK_SPACE]){......do ......something......here    keys[DIK_SPACE] = true;}else    keys[DIK_SPACE] = false;


Is this correct?

[EDIT1] this->buf is the buffer I am using to store what keys have been pressed. KEYDOWN = #define KEYDOWN(name, key) (name[key] & 0x80)

[EDIT2] No it's not..heh..back to work

[EDIT3] LAST EDIT, promise ;) Got it, I think:

if(KEYDOWN(this->buf,DIK_SPACE)&& !keys[DIK_SPACE])
{
......do
......something
......here

keys[DIK_SPACE] = true;
}
else if(!(KEYDOWN(this->buf,DIK_SPACE)))
keys[DIK_SPACE] = false;


Let me know if I am in error!
I WISH SOMEONE WOULD FIX THE DAMNED LOGIN!
Zook
Zook
The easiest way to do this is to use buffered input IMO. That will give you a messsage that a key has been pressed(or released) and which key it is. Since you only get a message when the state of the keyboard changes this will automatically solve your problem. Look up GetDeviceData (I'm pretty sure that's the name) in the SDK docs.
Pipo DeClown
Pipo DeClown
// NOT-down, right nowif(!KEYDOWN(this->buf,DIK_SPACE)) {    keys[DIK_SPACE] = false;}// Down, right nowelse {    // NOT-Locked    if(keys[DIK_SPACE] == false) {        // Do everything    }    // Locked    else {        // Do nothing    }}

This should work..
Nairou
Nairou
Another easy method, using immediate input, is to use double data buffers. Each time you poll the keyboard, you first dump the previous data into a secondary buffer (or just switch buffers). After you poll the keyboard for new data, compare the two buffers as you need them. If the new buffer says a key is down, and the old buffer says its not, then the user just now pressed it, so perform your action. If the data in both buffers is the same, the state of the key hasn't changed, so do nothing.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.