Advertisement

keyboard problem

Started by August 20, 2000 01:33 PM
21 comments, last by Rudan 24 years, 4 months ago
I''m using the base code without direct input from Nehe''s site in my project. So I use the keyboard like this:

if (keys[VK_SPACE])
{
	if (player.engine)
		player.engine = FALSE;
	else
		player.engine = TRUE;
}
 
but in the example above it''s to fast. Instead of changing from true to false or from false to true it loops through the code several times and might end up on the same value. How do I do to make sure it only changes one time, no matter how long i hold space down? Probably a stupid question but I have no idea...
-----------------------------Reporter: Are they slow-moving, chief?Sheriff: Yeah, they're dead. They're all messed up.-Night of the living dead
I don''t know exactly how you do it, but just execute that piece of code when you detect the space going UP not when it''s being pressed down.



Gyzmo
=======================
[signature][/signature]
Gyzmo=============================="Well is the world standard" - The Alchemist"Not in Canada!" - jonnyfish
Advertisement
That''s probably a good idea...
anyone know how?
-----------------------------Reporter: Are they slow-moving, chief?Sheriff: Yeah, they're dead. They're all messed up.-Night of the living dead
if player.engine is a boolean, I believe there is an operator that will inverse the value...

I *think* you could write the code:

    if ( keys[ VK_SPACE ] ){     player.engine = ^player.engine; // inverses the binary value (1 becomes 0 and 0 becomes 1)}    


I read that somewhere.. but now I''m not even sure that''s the right operator.

- Goblin
"A woodchuck would chuck as much wood as a woodchuck could if a woodchuck could chuck wood. Deal."
- The Goblin (madgob@aol.com)
Goblin: You're almost right.
Except the operator used for boolean operations must be ~
^ is used for bitwise XOR



Edited by - baskuenen on August 20, 2000 5:04:29 PM
If I've understood, you want change the flag only if you press space not if you continue to press it...?

You can store previous key state and detect transition up-down and ignore transition up-up, down-up and down-down.

        BOOL this_state,previous_state;previous_state = FALSE;...this_state = (GetKeyState(VK_SPACE)&0x8000);if(this_state&&!previous_state){   ...}previous_state = this_state;...        



Edited by - Andrea on August 21, 2000 11:08:48 AM
IpSeDiXiT
Advertisement
Andrea, your method seems logical but some reason i can't get it to work. I use DirectInput by the way...

                    UCHAR KeyState[256];UCHAR LastKeyState[256];if ((KeyState[DIK_P] & 0x80) && (!(LastKeyState[DIK_P] & 0x80))){	GameState = Paused;	for (int i = 0; i > 256; i++)	{	        LastKeyState[ i ]  = KeyState[ i ];	}//end if}//end if                    


Edited by - vbisme on August 20, 2000 12:54:13 AM
Very simple. What you do is you first detect when the key is down like you do in your code, and then set a boolean which is used for your if that describes whether the key is up or not. Confusing it soundsk but this sample code should clear it up.

bool keydown = false;
bool keyup = true;

if (keys[VK_SPACE])
{
if (keyup)
{
player.engine = !player.engine; // gives the inverse
// Instead of a whole if, else statement

keyup = false;
keydown = true;
}
}
else
{
if (keydown)
{
keyup = true;
keydown = false;
}
}

What that will do is check the first time around for a keyup, so if the key was just pressed, then keyup will still = true. Then keydown will be set and key up will be reset and the cycle begins again. That''s what I invented when I did a click for my mouse in DI. Hope I helped.

Oh and by the way, for the rest of you. I don''t know if
~(boolean) works, but that is what the not (!) operator is for.(and for conditionals). I do:

a = !a;
Oops...an error occurred !

We should check 15th bit in the WORD returned by
GetKeyState(VK_SPACE)

So you can use KEYDOWN() defined as

#define KEYDOWN(vkey) (GetKeyState(vkey)&0x8000)

If you want to use DirectInput for the keyboard try to use

memcpy(LastKeyState, KeyState, 256);

instead of

    for (int i = 0; i < 256; i++)	{    LastKeyState[ i ]  = KeyState[ i ];	}    


because it's faster

and put it outside the 'if' statement...in other words you must update LastKeyState every time you read KeyState.


Edited by - Andrea on August 21, 2000 11:18:54 AM

Edited by - Andrea on August 21, 2000 11:19:53 AM
IpSeDiXiT
so keydown is the 8th bit and keyup is the 15th bit?

by the way how does the 0x80 works?

how is 0x80 translate to the 8-bit byte and 0x8000 to 15-bit byte?

This topic is closed to new replies.

Advertisement