

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.




 
program Obtain_a_Y_or_N_response;
 
{$APPTYPE CONSOLE}
 
uses
  System.Console;
 
function GetKey(acepted: string): Char;
var
  key: Char;
begin
  while True do
  begin
    if Console.KeyAvailable then
    begin
      key := UpCase(Console.ReadKey().KeyChar);
      if pos(key, acepted) > 0 then
        exit(key);
    end;
  end;
  Result := #0; // Never Enter condition
end;
 
begin
  Console.WriteLine('Press Y or N');
  case GetKey('YN') of
    'Y':
      Console.WriteLine('You pressed Yes');
    'N':
      Console.WriteLine('You pressed No');
  else
    Console.WriteLine('We have a error');
  end;
  Readln;
end.