So I'm not 100% sure what you would call this type of keypress, but it is essentially a keyTyped event but in reverse order. Instead of being triggered when the button is released, it is triggered when the button is first pressed, but can't be triggered again until the button is released and pressed again.

Right now I have events for buttonDown and buttonUp and I also have two HashMaps to keep track of the currentInput and previousInput of each action in the game (jumping, moving, crouching, etc...) How can I provide a function that when called returns true for this type of button push?


You are almost there : you need to detect whether the key has just been pressed. So it is currently down, but wasn't an instant ago, which is easy to test since you have both states.

bool pressed(Action action) {
    return !previousInput[action] && currentInput[action];
  • \$\begingroup\$ That's what I have currently, the problem is that the previousInput doesn't update until the button is then released. In other words, as long as you keep the button held down, this method will continue to return true because the previous and current state of the input aren't being updated. \$\endgroup\$ – FaTal Cubez Aug 11 '16 at 13:57
  • \$\begingroup\$ @FaTalCubez I see. But then your previousInput is useless as-is : it can only contain the opposite of currentInput, since you only update it when there's a toggle. Or am I missing something ? \$\endgroup\$ – Quentin Aug 11 '16 at 14:05
  • \$\begingroup\$ I've been able to get it so that you have to lift up the key and press it again before repeating the action. However, my program still breaks when it comes to the case where you jump, mid-jump you release the key and press it again, and then when you land (still holding down the key), it jumps again. \$\endgroup\$ – FaTal Cubez Aug 11 '16 at 14:20

I suggest doing the following:

  1. Create an integer variable.
  2. Set it to zero if the current state is up.
  3. Increment it if the current state is down.
  4. Only activate the desired method when the value is zero and the state is down. Alternatively, if you increment prior to checking, only activate if and when the value is 1. Good luck!
  • \$\begingroup\$ I ended up just putting in an update() call that sets the previousInput to whatever value is in the currentInput. The update is called once a frame after checking for input. I then test to see if currentInput is true and previousInput is false. \$\endgroup\$ – FaTal Cubez Aug 12 '16 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.