HOW TO CHECK IF A KEY IS BEING HELD DOWN?

Posts

Pages: first 123 next last
You all know that in RPG Maker, if you have a key input, you store the value in a certain variable. The variable is set to the value of the key being pressed.

However, once you release the key the variable doesn't automatically reset to 0. This is problematic when designing systems where a key is being held down, because the effects of holding the button down remain even after releasing it.

So, is there a way to set the variable back to 0 if the key is not being held down?

This might actually be really simple, and I might just be missing it, and this might be a really dumb question. But I really can't think of it right now.


This works for me. (sorry if I misunderstand the problem:D)
LockeZ
I'd really like to get rid of LockeZ. His play style is way too unpredictable. He's always like this too. If he ran a country, he'd just kill and imprison people at random until crime stopped.
5958
You misunderstand the problem.

However, to solve the problem, set the variable to 0 at the beginning of the same parallel process that checks for key input.
See the reason this doesn't work is because once the key is pressed, the variable is set to 5 forever. Which means from that point the "else handler" is just going to get ignored.

The only way to let the game know the key is released, is to set the variable with the stored value to 0. But I don't know where to do that. I can't do it in the main part of the branch, because then as soon as the key is pressed it will think it's immediately released again. And it can't go in the else handler either, because the else handler will be ignored if the key has been pressed.

It can't go after the branch either, because then the events if the key is being pressed will just happen, and even if the player is holding the key, the value is still going to be reset to 0.
Hmm... when I release the Spacebar (key 5), the screen wont flash anymore, so I guess it gets reseted? This was tested in rpg maker 2003...

Edit: The debug mode shows 0 for the button variable, if I release it, so in fact, it gets reseted. At least in 2k3... (Not sure about the newer ones, but manual reset is the way to go)
Hmmm, that is weird. Where I'm using it, it does not get reset to 0. I guess the surrounding code is somehow preventing it...
Anyways, if manual reset is necessary, you should reset the variable before the Key Input Proc. command. That way, the variable will reset to 0 if you have no keys pressed.
LockeZ
I'd really like to get rid of LockeZ. His play style is way too unpredictable. He's always like this too. If he ran a country, he'd just kill and imprison people at random until crime stopped.
5958
Alternately, reset it after you're done doing whatever you do when they press the key.

Either way, don't put the reset in the else handler. Put it either in the branch that happens when the key is pressed, or put it outside the conditional branch.
Did you tick "Options: Wait until key hit" ?
Yeah, I did. Unfortunately that didn't help.

I think I'll mess around with it a bit more tomorrow, and try out your advice.
LockeZ
I'd really like to get rid of LockeZ. His play style is way too unpredictable. He's always like this too. If he ran a country, he'd just kill and imprison people at random until crime stopped.
5958
Post a screenshot of your common event if you can't figure it out.
Alright, but it's pretty damn huge lol. Well anyway here it is, the important part is marked with red.

What happens in that part is that it checks if Player's Choice is equal to 1. If it is, then player has rock. Then there's three instances for the CPU: It checks if it has 1, 2, or 3 (rock paper or scissors) and changes the result accordingly.

Now Player Choice 1 is the only one I've modified so far, in case you're wondering.

After player has gotten rock, and the player presses the left arrow (Defend), then the picture of the player changes to a defense position. If the key is released, it changes back to standing position (well, that's what's supposed to happen, and what I need help with).

As it is right now, once the left arrow key is pressed, the picture will always stay on defense position.

Picture
The variable you're setting to 0 is 0001. The variable you're checking in conditions in that box is 0005. No?
That variable has nothing to do with the Defend option. The 0001 variable checks if Shift is being pressed, and if Shift is being pressed then the thing starts scrolling. So it has to be set to 0 there.
I suppose I should point out that the hold key-option is not flawless. It wasn't in my game at least, judging by my own experience and several others.

I had a feature that charges an attack when you hold the button and maintains the state until released, but it would occasionally release the key-state when still holding the key. I first thought my code caused issues, but even something as simple as the code in the screen at the top had the weakness. Luckily enough, it's not a frequent problem. This was rm2k3.
Yeah, Rm2k3 drops the ball when it comes to complicated button stuff.

If only the Conditional Branch had options like "If following key is pressed" or "If following key is being held"...
I usually recommend a variable counter in this case. It's set to 0 when the key is reported as held and increased by 1 if it's reported as released (checked every frame, i.e. with a wait 0.0). If the counter reaches 3, we consider the key as "really releaseed".
But how would you be sure that the key is really released if it reaches 3?
author=JustRob
But how would you be sure that the key is really released if it reaches 3?

If you're asking what Cherry just said...

Have a separate check for whether the key is STILL being held

Parallel Process
If Variable(release counter) >= 3
- Set Variable (release counter) = 0
- call whatever code for stop defending

Set V(Defend) = 0

Key Input Process V(Defend)

If V(Defend) = 2
- Set V(release counter) = 0
Else
- Add V(release counter) +1

Wait 0.0

So every time through, if you're not holding LEFT, +1 to the counter for releasing defense. After 3 cycles, which is near instant, you'll release defense.

I'm pretty sure that's what Cherry meant. Correct me, if I'm wrong, please.

Edit:
I just looked at your code, and I don't really see where you're having the defend turn off at all. It looks like you have a problem getting the defend to turn off at all, and not having problems with the holdkey registering your input/lack of input.

You need to have Set Variable 0005 = 0 before the Key Input Proc(0005:Defend).
Yes, this is what I meant. 3 cycles are 0.05 seconds, and I have never seen the bug (RPG Maker reports 0 even though the key is still held down) happen 3 consecutive times (normally 1, I ONCE encountered 2 cycles), so you can be quite sure that after 3 cycles the key has indeed by released.
Pages: first 123 next last