# reaction.j - how fast are you? raw keys with the term libraryuse io;
use os;
use math;
use time;
use term;
if (notos.isTerminal("stdin")) {
io.printf("reaction needs a terminal\n");
exit1;
}
def state asterm.State initterm.makeRaw("stdin");
deferterm.restore($state);
io.printf("press any key when you see GO!\r\n");
# wait 1 to 3 seconds; a key pressed before GO! is a false startdef early asintinitterm.readByteTimeout(math.randInt(1000, 3000));
if ($early != -2) {
io.printf("too early!\r\n");
} else {
io.printf("GO!\r\n");
def start astime.Time inittime.now();
def b asintinitterm.readByteTimeout(5000);
if ($b == -2) {
io.printf("asleep? no key for 5 seconds\r\n");
} else {
def took astime.Duration inittime.sub(time.now(), $start);
io.printf("%d ms\r\n", time.milliseconds($took));
}
}