Recent

Author Topic: crt, wincrt and readkey  (Read 9754 times)

josip

  • New Member
  • *
  • Posts: 19
crt, wincrt and readkey
« on: January 21, 2018, 01:02:04 pm »
I have CLI FPC program that is running from terminal and sometimes need user input for YES / NO (readkey). Program can also be called by other GUI application that is not written by me (and in that case user input YES / NO is not important, not used) with it's own terminal / log window.

If I declare uses crt; than program executed from terminal (readkey) is working ok. However, in that case terminal / log window of GUI application is empty.

If I declare uses wincrt; than readkey in program executed from terminal is not working, while terminal / log inside GUI application is OK.

This problem I have is only related to Windows. On Linux and OS X, everything is working OK, readkey and terminal / log inside GUI.

Thaddy

  • Hero Member
  • *****
  • Posts: 14161
  • Probably until I exterminate Putin.
Re: crt, wincrt and readkey
« Reply #1 on: January 21, 2018, 02:03:04 pm »
use crt (you don't even need that: read/readln will do) and add {$apptype console} to your main lpr file or create a console manually. Don't use wincrt. You will still have a GUI application but with a command terminal window. Windows quirk.
« Last Edit: January 21, 2018, 02:05:54 pm by Thaddy »
Specialize a type, not a var.

josip

  • New Member
  • *
  • Posts: 19
Re: crt, wincrt and readkey
« Reply #2 on: January 21, 2018, 03:35:38 pm »
{$apptype console} didn't change it.

Read/readln can work without Crt, and without Crt GUI log window is working fine. However, in this case (with read/readln instead of readkey) user need to press Y + enter keys (for yes), and I prefer just to press Y key, if it is possible. And readkey is from Crt.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: crt, wincrt and readkey
« Reply #3 on: January 21, 2018, 10:01:22 pm »
However, in this case (with read/readln instead of readkey) user need to press Y + enter keys (for yes), and I prefer just to press Y key, if it is possible.
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3. program Project1;
  4.  
  5. uses Windows;
  6.  
  7. function ReadKey: Char;
  8. var
  9.   Mode: DWORD;
  10. begin
  11.   if GetConsoleMode(TextRec(Input).Handle, Mode) then
  12.     SetConsoleMode(TextRec(Input).Handle, Mode and not ENABLE_LINE_INPUT);
  13.   Read(Result);
  14.   SetConsoleMode(TextRec(Input).Handle, Mode);
  15. end;
  16.  
  17. var
  18.   C: Char;
  19. begin
  20.   Write('Press Y to continue: ');
  21.   repeat
  22.     C := ReadKey;
  23.     Write(C);
  24.   until UpCase(C) = 'Y';
  25. end.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: crt, wincrt and readkey
« Reply #4 on: January 22, 2018, 01:43:59 am »
What about USES Keyboard ???? No Windows Unit necessary....

Maybe something like this...
Code: Pascal  [Select][+][-]
  1. PROGRAM InputRightKey;
  2.  
  3.  USES
  4.   Crt, SysUtils, Keyboard;
  5.  
  6.  VAR
  7.   chKey: Char;
  8.  
  9.  
  10.  Function KeyInput: Char;
  11.    Var
  12.     Key: TKeyEvent;
  13.   Begin
  14.    InitKeyBoard;
  15.     Try
  16.      Key:= GetKeyEvent;
  17.      Key:= TranslateKeyEvent(Key);
  18.  
  19.      Result:= GetKeyEventChar(Key);
  20.     Finally
  21.      DoneKeyBoard;
  22.     End;
  23.   End;
  24.  
  25.  
  26. BEGIN
  27.  ClrScr;
  28.  Write('KeyInput:');
  29.  
  30.  Repeat
  31.   chKey:= KeyInput;
  32.  
  33.   Case chKey
  34.   Of
  35.    #27: Begin
  36.          WriteLn(sLineBreak+'ESC ... Bye Bye');
  37.          Sleep(2000);
  38.         End;
  39.  
  40.    #13: Begin
  41.          WriteLn(sLineBreak+'ENTER ... Bye Bye');
  42.          Sleep(2000);
  43.         End;
  44.   End;
  45.  Until (chKey = #27) Or (chKey = #13);
  46. END.


Code: Pascal  [Select][+][-]
  1. PROGRAM CheckKeys;
  2. {$APPTYPE CONSOLE}
  3. {$MODE OBJFPC}
  4.  
  5.  USES
  6.   Crt, SysUtils, Keyboard;
  7.  
  8.  VAR
  9.   booQuit: Boolean;
  10.   chKey  : Char;
  11.  
  12.  
  13.  Function KeyInput: Char;
  14.    Var
  15.     Key: TKeyEvent;
  16.   Begin
  17.    InitKeyBoard;
  18.     Try
  19.      Key:= GetKeyEvent;
  20.      Key:= TranslateKeyEvent(Key);
  21.  
  22.      Result:= GetKeyEventChar(Key);
  23.     Finally
  24.      DoneKeyBoard;
  25.     End;
  26.   End;
  27.  
  28.  
  29. BEGIN
  30.  While Not booQuit
  31.  Do
  32.   Begin
  33.    ClrScr;
  34.  
  35.    WriteLn(' MY PROGRAM');
  36.    WriteLn('============');
  37.    WriteLn('ESC   = Quit');
  38.    WriteLn('ENTER = Start');
  39.  
  40.    Repeat
  41.     chKey:= KeyInput;
  42.    Until (chKey = #27) Or (chKey = #13);
  43.  
  44.    Case chKey
  45.    Of
  46.     #27: Break; // ESC
  47.     #13:        // ENTER
  48.      Begin
  49.       While Not booQuit
  50.       Do
  51.        Begin
  52.         ClrScr;
  53.  
  54.         WriteLn(' MY PROGRAM');
  55.         WriteLn('============');
  56.         WriteLn;
  57.         WriteLn(' MAIN MENU');
  58.         WriteLn('===========');
  59.         WriteLn('N = Back');
  60.         WriteLn('Y = Show Version Number');
  61.  
  62.         Repeat
  63.          chKey:= KeyInput;
  64.         Until (chKey = 'n') Or (chKey = 'y');
  65.  
  66.         Case chKey
  67.         Of
  68.          'n': Break;
  69.          'y':
  70.           Begin
  71.            WriteLn;
  72.            WriteLn;
  73.            WriteLn('Version 1.0');
  74.            Sleep(1000);
  75.           End;
  76.         End;
  77.        End;
  78.      End;
  79.    End;
  80.   End;
  81. END.
« Last Edit: January 28, 2018, 12:56:59 am by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Thaddy

  • Hero Member
  • *****
  • Posts: 14161
  • Probably until I exterminate Putin.
Re: crt, wincrt and readkey
« Reply #5 on: January 22, 2018, 08:36:35 am »
What about USES Keyboard ???? No Windows Unit necessary....
You missed the original question: it is a combined GUI/CONSOLE App. Both mine (I use it myself all the time) and ASerge's solutions should work.
Using the Keyboard unit in such a scenario will mess up input in the GUI parts.
Specialize a type, not a var.

josip

  • New Member
  • *
  • Posts: 19
Re: crt, wincrt and readkey
« Reply #6 on: January 22, 2018, 11:16:28 am »
Thank you all.

I am using Windows unit, because of some other functions too.

I tried both solutions.
ASerge ReadKey function is still waiting for new line (enter key), same as standard Read.
RAW ReadKey function is working OK. In terminal mode it is waiting for key to be pressed (without new line enter key). There is working log present in GUI application.

Thaddy, GUI application just start terminal program and wait until it is finished (max few seconds), with disabled terminal / log window (readout only) GUI part. And during that time user doesn't need to press any keys, and I guess there is no issue with this. However, if there is working ASerge solution I can apply it.
« Last Edit: January 22, 2018, 11:21:47 am by josip »

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: crt, wincrt and readkey
« Reply #7 on: January 22, 2018, 03:49:12 pm »
ASerge ReadKey function is still waiting for new line (enter key), same as standard Read.
Very strange. On which version of Windows does my code wait for Enter?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: crt, wincrt and readkey
« Reply #8 on: January 22, 2018, 03:57:36 pm »
I create a file called "crttest"

Code: Pascal  [Select][+][-]
  1.  
  2. uses crt;
  3.  
  4. var k : char;
  5.  
  6. begin
  7.   while not keypressed do ;
  8.   k:=readkey;
  9. end.
  10.  

Then I compile with fpc <file> and click the resulting file in the file explorer. It works flawlessly.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: crt, wincrt and readkey
« Reply #9 on: January 22, 2018, 07:03:32 pm »
Quote
Very strange. On which version of Windows does my code wait for Enter?
Right now I'm on WINDOWS XP SP3 and I need to input RETURN ...
I can see the cursor directly after starting the program.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: crt, wincrt and readkey
« Reply #10 on: January 22, 2018, 07:44:53 pm »
Right now I'm on WINDOWS XP SP3 and I need to input RETURN ...
New version:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3. program Project1;
  4.  
  5. uses Windows;
  6.  
  7. function ReadKey: Char;
  8. var
  9.   Mode: DWORD = 0;
  10. begin
  11.   if GetConsoleMode(TextRec(Input).Handle, Mode) then
  12.     SetConsoleMode(TextRec(Input).Handle, 0);
  13.   Read(Result);
  14.   if Mode <> 0 then
  15.     SetConsoleMode(TextRec(Input).Handle, Mode);
  16. end;
  17.  
  18. var
  19.   C: Char;
  20. begin
  21.   Write('Press Y to continue: ');
  22.   repeat
  23.     C := ReadKey;
  24.     Write(C);
  25.   until UpCase(C) = 'Y';
  26. end.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: crt, wincrt and readkey
« Reply #11 on: January 22, 2018, 07:53:22 pm »
I can still see the cursor, but it's working without RETURN (XP SP3).
And without this line..
Quote
Write(C);
it works good.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

josip

  • New Member
  • *
  • Posts: 19
Re: crt, wincrt and readkey
« Reply #12 on: January 23, 2018, 05:46:05 pm »
Yes, now both functions are working OK, in terminal and inside the GUI. Problem solved. Thank you.

 

TinyPortal © 2005-2018