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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
No comments:
Post a Comment