I have done things similar to this a few times before:
First declare a few global variables:
var
hIn: THandle;
hTimer: THandle;
threadID: cardinal;
TimeoutAt: TDateTime;
WaitingForReturn: boolean = false;
TimerThreadTerminated: boolean = false;
Second, add functions
function TimerThread(Parameter: pointer): integer;
var
IR: TInputRecord;
amt: cardinal;
begin
result := 0;
IR.EventType := KEY_EVENT;
IR.Event.KeyEvent.bKeyDown := true;
IR.Event.KeyEvent.wVirtualKeyCode := VK_RETURN;
while not TimerThreadTerminated do
begin
if WaitingForReturn and (Now >= TimeoutAt) then
WriteConsoleInput(hIn, IR, 1, amt);
sleep(500);
end;
end;
procedure StartTimerThread;
begin
hTimer := BeginThread(nil, 0, TimerThread, nil, 0, threadID);
end;
procedure EndTimerThread;
begin
TimerThreadTerminated := true;
WaitForSingleObject(hTimer, 1000);
CloseHandle(hTimer);
end;
procedure TimeoutWait(const Time: cardinal);
var
IR: TInputRecord;
nEvents: cardinal;
begin
TimeOutAt := IncSecond(Now, Time);
WaitingForReturn := true;
while ReadConsoleInput(hIn, IR, 1, nEvents) do
if (IR.EventType = KEY_EVENT) and
(TKeyEventRecord(IR.Event).wVirtualKeyCode = VK_RETURN)
and (TKeyEventRecord(IR.Event).bKeyDown) then
begin
WaitingForReturn := false;
break;
end;
end;
Now you can use TimeoutWait
to wait for Return, but no longer than a given number of seconds. But you have to set hIn
and call StartTimerThread
before you make use of this function:
begin
hIn := GetStdHandle(STD_INPUT_HANDLE);
StartTimerThread;
Writeln('A');
TimeoutWait(5);
Writeln('B');
TimeoutWait(5);
Writeln('C');
TimeoutWait(5);
EndTimerThread;
end.
You can get rid of StartTimerThread
, especially if you start one thread per call, but it might be more tricky to call TimeoutWait
several times in a row then.
Readln
blocks execution flow until user presses <kbd>RETURN</kbd>. – Trek5
key.ReadLn
waits untilEnter
is pressed, so it won't work with the5
key unless you press5
and thenEnter
. But it's not clear what you're really asking here, and your code doesn't help. "Shows messages on button 5" and "Press ENTER to continue" don't seem to match. Please edit your question to make it more clear about what you're asking us. Thanks. – Acetaldehydewhile not KeyPressed do ...
. I should have linked this instead,How i can implement a IsKeyPressed function in a delphi console application?
. Arnaud got it right. – Fuchsin