Sign in to follow this  
Axesor

"When button is pressed"...

Recommended Posts

Axesor    100
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
jflanglois    1020
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
Axesor    100
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
Trillian    410
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this