Skip to main content
gamedev.net
🔒 Locked

Single input per press?

Started by Psykocyber Jun 14, 2009 at 5:22 PM 4 replies 750+ views
Original Post
Psykocyber
Psykocyber
I'm looking for some simple way of making sure my game doesn't receive a new input for the same key press. Currently I'm using a static bool for the job, but is there a more straightforward method?

//Key is a char
static bool KeyPressed = false;
if(!DInput->KeyDown(key))
	KeyPressed = false;	
else if(KeyPressed == false){
	KeyPressed = true;
	/*Code here*/
}

Something that could be used in a function so I can write something more clear than this and avoid redundant code would be nice. Thanks. (And that is the tag for posting code? ?)
SiCrane
SiCrane
Seeing as that looks like Windows code, then just handle WM_KEYDOWN messages. The code tags are [code] for short code segments and [source] for long code segments. Capitalization is important.
Psykocyber
Psykocyber
Is that enough? So pressing ESC wont hide/show continuously the menu until the user lets go of the button? And what about uses where I need several inputs like in an FPS game with WASD?
SiCrane
SiCrane
If you want to see if the WM_KEYDOWN message is a repeat check bit 30 of the lparam. I'm not sure what you're asking for the second question.
Psykocyber
Psykocyber
Thanks, I will look into that. You answered both questions in one answer

New problem, I would prefer not using a macro since we have an STL bit checker, but I'm not sure how I would use std::bitset in this situation.
http://www.cppreference.com/wiki/stl/bitset/start


[Edited by - Psykocyber on June 14, 2009 5:38:43 PM]
SiCrane
SiCrane
You can initialize a bitset with an integral type and check individual bits using operator[]. Ex:
  int x = 0xdeadbeef;  std::bitset<32> bs(x);  for (int i = 0; i < 32; i++) {    std::cout << bs;  }

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.