Friday, December 29, 2023

Delphi: Wait for ESCAPE key before continuing on console app

Delphi: Wait for ESCAPE key before continuing on console app

The sample Delphi console application below shows how to wait for the ESC key before it will continue.
program Project4;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Winapi.Windows;
procedure WaitForEscapeKey;
var
Done: Boolean;
Event: TInputRecord;
EventsRead: DWORD;
begin
Done := False;
repeat
ReadConsoleInput(GetStdhandle(STD_INPUT_HANDLE),
Event, 1, EventsRead);
if Event.Eventtype = key_Event then begin
if Event.Event.KeyEvent.bKeyDown then
begin
Done := Event.Event.KeyEvent.wVirtualKeyCode = VK_ESCAPE;
if (not Done) and (Event.Event.KeyEvent.AsciiChar <> #0) then
begin
WriteLn('You typed: ', Event.Event.KeyEvent.AsciiChar, ' - waiting for ESC');
Sleep(50);
end;
end;
end;
until Done;
end;
begin
try
Writeln('Press escape to end');
WaitForEscapeKey;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
view raw Project4.dpr hosted with ❤ by GitHub
Tags: Delphi, Console, Escape

No comments:

Running QNX on emulated cortex-a15 using QEMU

Running QNX on emulated cortex-a15 using QEMU General information: - Host (for running QEMU) OS: Ubuntu 24.04.2 LTS - QEMU: QEMU emulator...