Pedir al usuario la pulsación de una tecla

3706 vistas

Tenemos que usar el API de Windows ReadConsoleInput, después de recuperar el handle de la cónsola.



delphi
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils, Windows;
  7.  
  8. var
  9. han: THandle;
  10. dw: DWord;
  11. buf: TInputRecord;
  12. begin
  13. han := GetStdHandle(STD_INPUT_HANDLE);
  14. Write('¿ Continuar [S/N] ?');
  15. repeat
  16. FlushConsoleInputBuffer(han);
  17. dw := 0;
  18. ReadConsoleInput(han, buf, 1, dw);
  19. until (buf.EventType = KEY_EVENT) and (buf.Event.KeyEvent.AsciiChar in ['s','S','n','N']);
  20.  
  21. WriteLn(buf.Event.KeyEvent.AsciiChar);
  22.  
  23. case buf.Event.KeyEvent.AsciiChar of
  24. 's','S': WriteLn('¡¡ SI !!');
  25. 'n','N': WriteLn('¡¡ NON !!');
  26. end;
  27. ReadLn;
  28. end.