[Function] getPressedKeys

Started by Gudio, Jan 19, 2015, 02:32 PM

Previous topic - Next topic

Gudio

Since onPlayerGameKeysChange callback has been added to 0.4's squirrel module, you can write for example a nice spectator mode with sprites or textdraws that show what keys are pressed by the player at the moment.
Function is provided below:

onfoot <- [ KEY_ONFOOT_FORWARD, KEY_ONFOOT_BACKWARD, KEY_ONFOOT_LEFT, KEY_ONFOOT_RIGHT, KEY_ONFOOT_JUMP, KEY_ONFOOT_SPRINT, KEY_ONFOOT_FIRE, KEY_ONFOOT_CROUCH, KEY_ONFOOT_PUNCH, KEY_ONFOOT_PREVWEP, KEY_ONFOOT_NEXTWEP, KEY_ONFOOT_AIM ];
incar <- [ KEY_INCAR_LEFT, KEY_INCAR_RIGHT, KEY_INCAR_BACKWARD, KEY_INCAR_FORWARD, KEY_INCAR_HORN, KEY_INCAR_LEANUP, KEY_INCAR_LEANDOWN, KEY_INCAR_LOOKLEFT, KEY_INCAR_LOOKRIGHT ];


function getPressedKeys( keysFlag, isOnFoot )
{
local table, pressedKeys = {}, i = 0;


if ( isOnFoot )
table = onfoot;
else
table = incar;


foreach ( key in table )
{
if ( key <= keysFlag )
{
pressedKeys.rawset( i++, key );
keysFlag -= key;
}
}


return pressedKeys;
}

Have fun and grab the latest version of squirrel module here.