Recent

Author Topic: [SOLVED] Prevent left, right, up, down key to be printed in console.  (Read 7371 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Hello.

With this program:

Code: Pascal  [Select][+][-]
  1. program test_crt;
  2. begin
  3.   readln();
  4. end.

If you press left, right, up, down key, this will be printed:

Quote
fred@fredvs ~> ./test_crt
^[[D^[[B^[[A^[[B

I dont want this.

There is a solution, just add crt in uses section.

Code: Pascal  [Select][+][-]
  1. program test_crt;
  2. uses
  3. crt;
  4.  
  5. begin
  6. readln();
  7. end.
  8.  

Doing this, nothing is printed when using left, right, up, down key.

Perfect but...

If a error is raised by fpc and crt was not used, like this:

Code: Pascal  [Select][+][-]
  1. program test_crt;
  2. var
  3. x : integer = 0;
  4. begin
  5. readln();
  6. x := 10 div x; // raise error well aligned
  7. end.

There is still the printed char when left, right, up, down key is pressed but the error appears ok:

Quote
fred@fredvs ~> ./test_crt
^[[D^[[C^[[D^[[D
An unhandled exception occurred at $000000000040110A:
EDivByZero: Division by zero
  $000000000040110A

Now, adding crt, no printed char with left, right, up, down key but the error message is not correctly show:

Code: Pascal  [Select][+][-]
  1. program test_crt;
  2.  
  3. uses
  4. crt; // adding crt
  5.  
  6. var
  7. x : integer = 0;
  8. begin
  9. readln();
  10. x := 10 div x; // raise error not aligned
  11. end.

Quote
fred@fredvs ~> ./test_crt
An unhandled exception occurred at $000000000040110A:
                                                     EDivByZero: Division by zero
   $000000000040110A

                    ⏎

Is it possible to have both: no print of left, right, ... char and also good layout of error when using crt.

Or maybe there is a solution without using crt to not print left, right, ... char.

Thanks.

Fre;D
« Last Edit: November 28, 2021, 05:08:00 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Prevent left, right, up, down key to be printed in console.
« Reply #1 on: November 24, 2021, 12:31:16 pm »
Not sure why it renders the message that way, but use try ... except to catch the exception, so you can write it down properly.  I know it is more code but you should manage the exceptions anyway.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Prevent left, right, up, down key to be printed in console.
« Reply #2 on: November 24, 2021, 03:13:32 pm »
Maybe (untested), use crt and on the toplevel add a try finally around everything like this:

Code: Pascal  [Select][+][-]
  1. try
  2.  // main program code.
  3. finally
  4. assign(output,'');
  5. rewrite(output);
  6. end;
  7.  

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: Prevent left, right, up, down key to be printed in console.
« Reply #3 on: November 24, 2021, 04:56:58 pm »
Hello Ñuño and Marcov.

@Ñuño, yes good idea, but like you said, not easy to implement.

@Marcov, sorry, I dont see where to place the code  :-[ .

It is about fpd:
https://gitlab.com/ccrause/lazarus/-/tree/fpd/components/fpdebug/app/fpd

Now it works like charm, the only detail is those characters printed with the left, right, top, down keys.
OK to use crt but I did not catch where to place the try finally.

I have seen that there are lot of things done in initialization of crt.
Could it be that something is wrong there ?

Here code of initialization in crt.pp

Code: Pascal  [Select][+][-]
  1.  
  2. Initialization
  3. {$ifdef debugcrt}
  4.   Assign(DebugFile,'debug.txt');
  5.   ReWrite(DebugFile);
  6. {$endif}  
  7. { Redirect the standard output }
  8.   assigncrt(Output);
  9.   Rewrite(Output);
  10.   TextRec(Output).Handle:=StdOutputHandle;
  11.   assigncrt(Input);
  12.   Reset(Input);
  13.   TextRec(Input).Handle:=StdInputHandle;
  14. { Are we redirected to a file ? }
  15.   OutputRedir:= IsAtty(TextRec(Output).Handle)<>1;
  16. { does the input come from another console or from a file? }
  17.   InputRedir :=
  18.    (IsAtty(TextRec(Input).Handle)<>1) or
  19.    (not OutputRedir and
  20.     (TTYName(TextRec(Input).Handle) <> TTYName(TextRec(Output).Handle)));
  21. { Get Size of terminal and set WindMax to the window }
  22.   GetConsoleBuf;
  23.   WindMinX:=1;
  24.   WindMinY:=1;
  25.   WindMaxX:=ScreenWidth;
  26.   WindMaxY:=ScreenHeight;
  27.   WindMax:=((ScreenHeight-1) Shl 8)+(ScreenWidth-1);
  28. {Get Current X&Y or Reset to Home}
  29.   if OutputRedir then
  30.    begin
  31.      CurrX:=1;
  32.      CurrY:=1;
  33.    end
  34.   else
  35.    begin
  36.    { Set default Terminal Settings }
  37.      SetRawMode(True);
  38.    { Get current X,Y if not set already }
  39.      GetXY(CurrX,CurrY);
  40.      if (CurrX=0) then
  41.       begin
  42.         CurrX:=1;
  43.         CurrY:=1;
  44.         ttySendStr(#27'[H');
  45.       end;
  46.    {Reset Attribute (TextAttr=7 at startup)}
  47.       ttySendStr(#27'[m');
  48.     end;


And does it exist a alternative to crt for those printed keys?

This happens on Linux, I did not test on Windows yet.

Thanks.

Fre;D
« Last Edit: November 24, 2021, 05:13:34 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: Prevent left, right, up, down key to be printed in console.
« Reply #4 on: November 25, 2021, 11:58:05 pm »
Maybe (untested), use crt and on the toplevel add a try finally around everything like this:

Code: Pascal  [Select][+][-]
  1. try
  2.  // main program code.
  3. finally
  4. assign(output,'');
  5. rewrite(output);
  6. end;
  7.  

OK, I did this:

Code: Pascal  [Select][+][-]
  1. procedure TFPDLoop.DoRun;
  2. var
  3.   S: String;
  4.   b: boolean;
  5. begin
  6. try
  7.   Write('FPD ' + inttostr(StepNum) + ' ~> ');
  8.   ReadLn(S);
  9.   if trim(S) <> ''
  10.   then FLast := S;
  11.   if FLast <> '' then
  12.     begin
  13.     HandleCommand(FLast, b);
  14.     if b then inc(StepNum);
  15.     while b do
  16.       begin
  17.       GController.ProcessLoop;
  18.       GController.SendEvents(b);
  19.       end;
  20.     end;
  21.  
  22. finally
  23. system.assign(output,'');
  24. rewrite(output);
  25. end;
  26.  
  27. end;

There are 3 images in attach:

Without using crt, all is printed correctly.  (but left, right key is printed)
With crt, the fpc error message is not correctly aligned. (but left, right key NOT printed)
With ctr + try/assign, both fpd + fpc messages are not aligned.  (but left, right key NOT printed)

 :-\

Fre;D
« Last Edit: November 26, 2021, 12:02:07 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: Prevent left, right, up, down key to be printed in console.
« Reply #5 on: November 26, 2021, 03:48:51 pm »
Hello.

Sorry  to annoy you back with that annoying detail of the fpc error messages not aligned.

I did try using "keyboard" unit but did not help, the fpc messages are still not aligned.
With 'try'..'expect', also no luck.

So I explore other ways, still using crt.

One solution that works is to remove the fpc error message from the screen and redirect to a file.  :-X

With this it is OK, the fpc error message are ignored (and in the file, the erorr is aligned !!!).

 
Code: Pascal  [Select][+][-]
  1. program mytmessage_redirect;
  2.   uses
  3.      ...,  Classes, BaseUnix;
  4.      var
  5.         fs: TFileStream;
  6.       begin
  7.          fs := TFileStream.Create('error.log', fmOpenReadWrite or fmCreate);
  8.          FpDup2(fs.Handle, StdErrorHandle);
  9.           ...
  10.          Application.run;
  11.          fs.free;
  12.       end.

But I would prefer to keep also the fpc error on screen.

Is there a way to do a FpDup2 and redirect to a string variable instead of a file ?
And then re-format the string to be correctly shown and then print it on screen?

Fre;D

PS: There is a bug, imho, in fpc error message rendering when crt is used, did you try the first example of my first post ?
« Last Edit: November 26, 2021, 04:02:57 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: Prevent left, right, up, down key to be printed in console.
« Reply #6 on: November 28, 2021, 05:07:26 am »
Hum, lot of noise here!  :-X

After big fight, finally I get a solution.

Without using crt but using keyboard unit.
 
The trick is to use instead of

Code: Pascal  [Select][+][-]
  1. Readln(S);

use this:

Code: Pascal  [Select][+][-]
  1. S := mygetkeys(S);  

And here is the function mygetkeys used:
 
Code: Pascal  [Select][+][-]
  1. function mygetkeys(prompt: string) : string;
  2. const
  3.   CHARS = ['=', '+', '-', '.', '/', ':', '\' , '0'..'9', 'a'..'z', 'A'..'Z'];
  4. var
  5.   s : string = '';
  6.   stmp : string = '';
  7.   i : integer;
  8.   key : char = #00 ;
  9.   kw : word ;
  10.   k : TKeyEvent;
  11. begin
  12.  
  13.   InitKeyBoard;  // important
  14.  
  15.   while Key <>  #13 do // enter not pressed
  16.     begin
  17.    
  18.       K:=GetKeyEvent;
  19.       K:=TranslateKeyEvent(K);
  20.       key := GetKeyEventChar(K);
  21.    
  22.     case key of
  23.       #00 : begin
  24.                K:=GetKeyEvent;
  25.                K:=TranslateKeyEvent(K);  
  26.                kw := GetKeyEventCode(K);
  27.                case kw of  
  28.                  65313 :  write ('UP'); // Up Arrow key pressed
  29.                  65315 :  write ('LEFT'); // Left Arrow key pressed    
  30.                  65317 :  write ('RIGHT'); // right Arrow key pressed
  31.                  65319 :  write ('DOWN'); // down Arrow key pressed
  32.                 end;
  33.              end;
  34.  
  35.        #8 : begin // backspace
  36.             stmp := '';
  37.             for i := 1 to length(s) + length(prompt) do stmp := stmp + ' ';
  38.             Write(#13+ stmp);
  39.              s := system.copy(s,1, length(s) - 1);
  40.              Write(#13+ prompt + s);
  41.             end;        
  42.        else
  43.          begin
  44.          if (key in CHARS) then
  45.          begin
  46.          S := s + char(key);  
  47.           Write( key);
  48.          end;
  49.        end;        
  50.       end;
  51.    end;
  52.  
  53.    DoneKeyBoard; // important otherwise fpc message is not aligned.
  54.    
  55.    result := s;
  56.  
  57. end;

Fre;D
« Last Edit: November 28, 2021, 05:56:48 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018