var tap_counter;
var run_speed;
...
while (1) {
if (key_cur || key_cul) {
tap_counter = 0;
while (key_cur || key_cul) { wait(1); } //wait for the key to be released
while (!key_cur && !key_cul) { tap_counter += 1 * time_step; wait(1); } //wait for the key to be pressed again, activate counter
run_speed = (key_cur - key_cul) * (1/tap_counter); //calculate direction and multiply by the 'slowness' of pressing key
}
wait(1);
}