"When button is pressed"...

Started by
3 comments, last by Axesor 17 years, 12 months ago
How do you make it where you can only press a button once then you have to let go to receive action? Example: Without one-press action -> Gun shoots non-stop With one-press action -> Gun only shoots once until you press the button again. So how do you do this with a similar: if(GetKeyState(VK_KEY) & 0x80 Thank you, Axesor
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward
Advertisement
You want to keep a previous key state. That way you can figure out what "action" happened to a key.

Example: Say the 'A' key shoots.

Previous state: 'A' key up
Current state: 'A' key down
Event: 'A' key press
Processing:
  If weapon is automatic, start shooting
  Otherwise, fire single shot

Previous state: 'A' key down
Current state: 'A' key down
Event: 'A' key held down
Processing:
  If weapon is automatic, continue shooting
  Otherwise, do nothing

Previous state: 'A' key down
Current state: 'A' key up
Event: 'A' key release
Processing:
  If weapon is automatic, stop shooting
  Otherwise, do nothing

Previous state: 'A' key up
Current state: 'A' key up
Event: 'A' key not pressed
Processing: Do nothing

Hope this helps.


jfl.
I know this. This is what I am asking. How would you program it though? I think you would use a boolean ,but how would it read if the key is let go?
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward
What about :

bool oldstate = false;void Update(){bool newstate = (GetKeyState(VK_KEY) & 0x80) != 0;if(!oldstate && newstate)  { /*manage key just pressed*/   }if(oldstate  && newstate)  { /*manage key still pressed*/  }if(oldstate  && !newstate) { /*manage key just released*/  }if(!oldstate && !newstate) { /*manage key still released*/ }oldstate = newstate;}


Is this what you wanted? a source code sample?
Actually, I just needed clarafication and a small code snippet to fully understand, but thanks. I'll try that.
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward

This topic is closed to new replies.

Advertisement