

Task

Obtain a valid   Y   or   N   response from the keyboard.

The keyboard should be flushed, so that any outstanding key-presses are removed, preventing any existing   Y   or   N   key-press from being evaluated.

The response should be obtained as soon as   Y   or   N   are pressed, and there should be no need to press an   enter   key.




import "io" for Stdin, Stdout
 
Stdin.isRaw = true // input is neither echoed nor buffered in this mode
 
System.print("Press Y or N")
Stdout.flush()
 
var byte
while ((byte = Stdin.readByte()) && !"YNyn".bytes.contains(byte)) {}
var yn = String.fromByte(byte)
System.print(yn)
 
Stdin.isRaw = false