GetAsyncKeyState how to properly use it?

Posts 1–15 of 20 · Page 1 of 2
GetAsyncKeyState how to properly use it?
I am working on sprites atm but i cant make them be there without me always holding down the Insert key..

My code:

Code:
	if(Sprite==true)
	{
		pSprite->Begin(D3DXSPRITE_ALPHABLEND);
		pSprite->Draw(pTexture, NULL, NULL, &Position, 0xFFFFFFFF);
		pSprite->End();
	}


	if (GetAsyncKeyState(VK_INSERT)<0){
		Sprite=true;
	}else{
		Sprite=false;
	}
If you can help i will be greatly thanksfull and +rep you
the problem is on your else..? if you don't have it pressed it will be false.. which "if(Sprite==true)" will be negative

edit: you will have to play around with those bools.. not hard

Code:
	if (GetAsyncKeyState(VK_INSERT)<0)
		Sprite = !Sprite
yeah thats what i mean i want it to when i press it one time its up and when i press again it goes down...
Quote Originally Posted by Mr.Magicman View Post
yeah thats what i mean i want it to when i press it one time its up and when i press again it goes down...
just edited my post, anyway not sure if the var will be saved.. so might not work, it depends where bool is at.
Quote Originally Posted by 'Bruno View Post
just edited my post, anyway not sure if the var will be saved.. so might not work, it depends where bool is at.
+rep and thanks!
Quote Originally Posted by Mr.Magicman View Post
I am working on sprites atm but i cant make them be there without me always holding down the Insert key..

My code:

Code:
	if(Sprite==true)
	{
		pSprite->Begin(D3DXSPRITE_ALPHABLEND);
		pSprite->Draw(pTexture, NULL, NULL, &Position, 0xFFFFFFFF);
		pSprite->End();
	}


	if (GetAsyncKeyState(VK_INSERT)<0){
		Sprite=true;
	}else{
		Sprite=false;
	}
If you can help i will be greatly thanksfull and +rep you
Try this out.

Code:
if(GetAsyncKeyState(VK_INSERT)<0){
     if(Sprite==0){
     Sprite = 1;
     Sleep(200); //so that you don't turn it on and off in the same keydown
      }
     else(
     Sprite = 0;
     Sleep(200);
     }
}
At the moment (assuming your code is in a loop) it is checking if the insert key is being pressed down each time it loops and if it is, set sprite to true, and if it isn't being held down set it to false. This is why you have to hold down the insert key for it to work. In my code however, if the user presses insert, it checks if sprite is currently == 0, if so set the value to 1, if it's already 1, set it to 0 and so on.

Do you get it?
Quote Originally Posted by Jason View Post


Try this out.

Code:
if(GetAsyncKeyState(VK_INSERT)<0){
     if(Sprite==0){
     Sprite = 1;
     Sleep(200); //so that you don't turn it on and off in the same keydown
      }
     else(
     Sprite = 0;
     Sleep(200);
     }
}
At the moment (assuming your code is in a loop) it is checking if the insert key is being pressed down each time it loops and if it is, set sprite to true, and if it isn't being held down set it to false. This is why you have to hold down the insert key for it to work. In my code however, if the user presses insert, it checks if sprite is currently == 0, if so set the value to 1, if it's already 1, set it to 0 and so on.

Do you get it?
Your using too much code for something simple

if (GetAsyncKeyState(VK_INSERT)&1)
Sprite=!Sprite;
Quote Originally Posted by Jason View Post


Try this out.

Code:
if(GetAsyncKeyState(VK_INSERT)<0){
     if(Sprite==0){
     Sprite = 1;
     Sleep(200); //so that you don't turn it on and off in the same keydown
      }
     else(
     Sprite = 0;
     Sleep(200);
     }
}
At the moment (assuming your code is in a loop) it is checking if the insert key is being pressed down each time it loops and if it is, set sprite to true, and if it isn't being held down set it to false. This is why you have to hold down the insert key for it to work. In my code however, if the user presses insert, it checks if sprite is currently == 0, if so set the value to 1, if it's already 1, set it to 0 and so on.

Do you get it?
thats what Sprite = ! Sprite does mate

btw i agree with the Sleep, forgot that. Yea you should use it.
Quote Originally Posted by 'Bruno View Post
thats what Sprite = ! Sprite does mate

btw i agree with the Sleep, forgot that. Yea you should use it.
Whatever, sorry for trying to help.
Your logic fails:

if (GetAsyncKeyState(VK_INSERT)<0)

if (value) is less than zero? I will stop there.
Quote Originally Posted by freedompeace View Post
Your logic fails:

if (GetAsyncKeyState(VK_INSERT)<0)

if (value) is less than zero? I will stop there.
? normaly people could use like != 0... but if the values never goes higher then 0.. what wrong with it? oO

Quote Originally Posted by Jason View Post


Whatever, sorry for trying to help.
you did right ^^ i did not say you did something wrong, actually i agree with what you said with the sleep and the rest, i just wrote it a little smaller, but it is correct
Quote Originally Posted by 'Bruno View Post
? normaly people could use like != 0... but if the values never goes higher then 0.. what wrong with it? oO



you did right ^^ i did not say you did something wrong, actually i agree with what you said with the sleep and the rest, i just wrote it a little smaller, but it is correct
Yay. Well I've never really worked with bool properly, and I keep using int because im used to having to say "This = new" no matter if it's boolean or integer in VB

i.e in VB you can't do

BooleanVariable = !BooleanVariable i don't think

so I usually just do an integer 1 or 0 cos I like them. I'm applying the same reasoning to C++ even though i guess there are different and simpler methods. Was my concept right in my word description? was that what was causing the menu problems?
Quote Originally Posted by Jason View Post


Yay. Well I've never really worked with bool properly, and I keep using int because im used to having to say "This = new" no matter if it's boolean or integer in VB

i.e in VB you can't do

BooleanVariable = !BooleanVariable i don't think

so I usually just do an integer 1 or 0 cos I like them. I'm applying the same reasoning to C++ even though i guess there are different and simpler methods. Was my concept right in my word description? was that what was causing the menu problems?
well i thought that too (what you wrote), i think that the problem was that
Quote Originally Posted by 'Bruno View Post
? normaly people could use like != 0... but if the values never goes higher then 0.. what wrong with it? oO
The OP's code had a logic fail, and it appears as though you didn't recognise it either. That was the reason why his code didn't work.

Code:
if (GetAsyncKeyState(VK_INSERT)<0)
Okay. Now, we know that GetASnycKeyState returns either 0 (false) or 1 (true), and that's the scope of it's returning values. Knowing this, that code would always evaluate to false, because it is never less than zero. The lowest possible value is zero. Zero is not less than zero, and therefore the statement will always evaluate to false.
Posts 1–15 of 20 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us