• Advertisement
Sign in to follow this  

"When button is pressed"...

This topic is 4290 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Actually, I just needed clarafication and a small code snippet to fully understand, but thanks. I'll try that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement