ID:951143
 
Keywords: double, key, press, tap
(See the best response by GreatFisher.)
Hi guys, is it possible to check if the player has pressed the key twice in a row? I'm trying to incorporate a 'dash' feature where it throws the player forward when they double-press a key.

Is there a built in proc or something for this? If not, what's the most efficient way to code one?

Thanks
Best response
You could use Forum_Account's keyboard library, make a variable to store the last key pressed and check it against the next key pressed. You could then possibly make a proc called double press or something like that to handle the behavior of that double press. You may also want to reset the variable to null after like a second.
Okay thanks that worked. Atm I've got:

            var/d = 0
var/last_key

if(keys["north"])
d |= NORTH
last_key = "north"
if(last_key == "north")
src << "double press"
last_key = "reset"

step(mob, d)
spawn(10)
if(last_key == "reset")
last_key = "reset"
src<<"[last_key]"


It works fine, the problem is if the user holds the same key down, it counts it as them pressing it again instead.

Could someone write me a code to check if the key is being held down? Don't add it into my code please, I'd rather learn myself.

Thanks
Keep a list of keys held. On KeyDown, add the key to the list. Take it out on KeyUp.
Thanks I'll try that out.
Using Forum_account's Keyboard library, you have access to the key_down() and key_up() events. For double-tapping, you really only need to worry about key_down(), but it depends on what you mean by double-tapping. Here's what I've done:
mob
var
tmp
last_key // What key was pressed?
double_tap_time // When should it be pressed again to count as a double-tap?
double_tap_sensitivity = 2 // How long can you take?

key_down(key)
if(last_key == key && world.time < double_tap_time)
double_tap(key)

else
last_key = key
double_tap_time = world.time + double_tap_sensitivity
..()

proc/double_tap(key)
src << "You double-tapped [key]!"

The key-downs of a key must happen no more than 1/5th of a second apart. The double_tap(key) event is triggered when a successful double-tap has been performed.
Thanks that worked great.