ID:195056
 
//Title: 4.0 Key Tapping, Key Holding, Key Up, Key Down
//Credit to: Jtgibson
//Contributed by: Jtgibson


/*
BYOND 4.0 adds some powerful features for determining whether a key is held down
versus whether a key is tapped. If you're making a single player game and want
to have some fine control over the four possible key states, just include this
snippet in your project.

Then call ProcessTrigger(keyID) with your keydown verb and ProcessRelease(keyID)
with your keyup verb, and make your macro sheet call the keydown() and keyup()
verbs with appropriate keyIDs as the argument.

I don't really recommend this for internet play, since transmission latency will
make things feel less precise to the client.
*/



client
var/list/keys_pressed = list()
var/list/time_pressed = list()

var/list/held_keys = list()


client/proc/ProcessTrigger(keyID)
if(!(keyID in keys_pressed)) keys_pressed += keyID
time_pressed[keyID] = world.time
DepressedKey(keyID)
spawn(2)
if(keyID in keys_pressed)
HoldingKey(keyID)
if(!(keyID in held_keys)) held_keys += keyID

client/proc/ProcessRelease(keyID)
if(time_pressed[keyID] >= world.time-1) TappedKey(keyID)
else ReleasedKey(keyID)
keys_pressed -= keyID
time_pressed -= keyID
held_keys -= keyID


client/proc
//A "depress" happens when you push the key down.
DepressedKey(keyID)
usr << "PRESSED KEY [keyID]"

//A "tap" happens if you release the key within 0.1 seconds of depressing it.
TappedKey(keyID)
usr << "TAPPED KEY [keyID]"

//A "holding" happens if you do not release the key within 0.1 seconds of depressing it.
HoldingKey(keyID)
usr << "HOLDING KEY [keyID]"

//A "release" happens if you release the key after holding it.
ReleasedKey(keyID)
usr << "RELEASED KEY [keyID]"