357mag 2 Posted May 3, 2023 What are some of the ways I can code my console program to keep the console window open after it opens? I know in C++ I used to use getch(). Share this post Link to post
programmerdelphi2k 237 Posted May 3, 2023 (edited) "ReadLN" dont? or https://stackoverflow.com/questions/2591774/how-to-create-a-console-application-that-does-not-terminate Edited May 3, 2023 by programmerdelphi2k Share this post Link to post
357mag 2 Posted May 3, 2023 Yes I was about to post that I found a way using ReadLine. I'm a little confused about the two end keywords. It seems sometimes I see end with a semicolon and that is all. Other times I see end with a period. Other times I see both in a code snippet. Share this post Link to post
DelphiUdIT 176 Posted May 3, 2023 (edited) And something like this ? program Test; {$APPTYPE CONSOLE} {$R *.res} Uses System.SysUtils, WinApi.Windows; var Termina: boolean = false; function handle_ctrl_c(fdwCtrlType: UINT64): WORDBOOL; begin //If it returns TRUE then the chain of managers is not continued. //If I return FALSE then the next handler in the chain is also called. case fdwCtrlType of // Handle the CTRL-C signal. CTRL_C_EVENT: begin Termina := True; Beep(750, 300); writeln('Ctrl-C event'); result := TRUE; end; // CTRL-CLOSE: confirm that the user wants to exit. CTRL_CLOSE_EVENT: begin Termina := True; Beep(600, 200); writeln('Ctrl-Close event'); result := TRUE; //The program will' close ... end; // Pass other signals to the next handler. CTRL_BREAK_EVENT: begin Termina := True; Beep(900, 200); writeln('Ctrl-Break event'); result := TRUE; end; CTRL_LOGOFF_EVENT: begin Termina := True; Beep(1000, 200); writeln('Ctrl-Logoff event'); result := FALSE; end; CTRL_SHUTDOWN_EVENT: begin Termina := True; Beep(750, 500); writeln('Ctrl-Shutdown event'); result := FALSE; end; else result := FALSE; end; end; //Main begin //Implements ctrl-... handler SetConsoleCtrlHandler(@handle_ctrl_c, TRUE); Writeln(DateTimeToStr(now)+' - Is running - press CTRL-C to terminate or the key "F2"'); while (not Termina) do begin try sleep(10); //This is another way to catch a key //Pressing F2 key will exit if (GetKeyState(VK_F2) and $8000) <> 0 then begin Termina := true; //This is not necessary since there is a break condition break; end; //put here your code except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end; end. Edited May 3, 2023 by DelphiUdIT Share this post Link to post
357mag 2 Posted May 3, 2023 So when your program is all completed as far as writing all the code, you put an end (with a period) at the end. If you are just denoting a code block, then you put and end (with a semicolon). Share this post Link to post
DelphiUdIT 176 Posted May 3, 2023 (edited) 31 minutes ago, 357mag said: So when your program is all completed as far as writing all the code, you put an end (with a period) at the end. If you are just denoting a code block, then you put and end (with a semicolon). Now is OK, with proper heading and better indentation. You must terminate with a "end." (end with a period) The last begin is closed in this way in an "app console" (if you look a dpr file of a vcl application, you will see the same ... begin .... end.) Edited May 3, 2023 by DelphiUdIT Share this post Link to post
Remy Lebeau 1394 Posted May 4, 2023 (edited) On 5/3/2023 at 11:28 AM, 357mag said: I'm a little confused about the two end keywords. It seems sometimes I see end with a semicolon and that is all. Other times I see end with a period. Other times I see both in a code snippet. 'end;' terminates compound statements, case statements, scope blocks, class/record declarations, etc. See Declarations and Statements (Delphi) 'end.' terminates only a unit block. See Programs and Units (Delphi) You should read the Delphi Language Guide for all the syntax rules. Edited May 4, 2023 by Remy Lebeau Share this post Link to post