-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathVelthuis.AutoConsole.pas
60 lines (49 loc) · 1.13 KB
/
Velthuis.AutoConsole.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
unit Velthuis.AutoConsole platform;
interface
implementation
uses
Winapi.Windows;
function GetConsoleWindow: HWnd; stdcall;
external 'kernel32.dll' name 'GetConsoleWindow';
procedure WaitForInput;
var
InputRec: TInputRecord;
NumRead: Cardinal;
OldMode: DWORD;
StdIn: THandle;
begin
StdIn := GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(StdIn, OldMode);
SetConsoleMode(StdIn, 0);
repeat
ReadConsoleInput(StdIn, InputRec, 1, NumRead);
until (InputRec.EventType and KEY_EVENT <> 0) and InputRec.Event.KeyEvent.bKeyDown;
SetConsoleMode(StdIn, OldMode);
end;
function StartedFromConsole: Boolean;
var
ConsoleHWnd: THandle;
ProcessId: DWORD;
begin
ConsoleHwnd := GetConsoleWindow;
if ConsoleHWnd <> 0 then
begin
GetWindowThreadProcessId(ConsoleHWnd, ProcessId);
Result := GetCurrentProcessId <> ProcessId;
end
else
Result := False;
end;
procedure Pause;
begin
if IsConsole and not StartedFromConsole then
begin
Writeln;
Write('Press any key... ');
WaitForInput;
end;
end;
initialization
finalization
Pause;
end.