[SOLVED] Prevent left, right, up, down key to be printed in console. lazarus / 62-General ---------------------------------------------------------------------- Fred vS · 2021-11-23 17:41 Hello. With this program: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test_crt;begin  readln();end. If you press left, right, up, down key, this will be printed: Quotefred@fredvs ~> ./test_crt ^[[D^[[B^[[A^[[B I dont want this. There is a solution, just add crt in uses section. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test_crt;usescrt; beginreadln();end.  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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test_crt;varx : integer = 0;beginreadln();x := 10 div x; // raise error well alignedend. There is still the printed char when left, right, up, down key is pressed but the error appears ok: Quotefred@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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test_crt; usescrt; // adding crt varx : integer = 0;beginreadln();x := 10 div x; // raise error not alignedend. Quotefred@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 ---------------------------------------------------------------------- Ñuño_Martínez · 2021-11-24 11:31 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. ---------------------------------------------------------------------- marcov · 2021-11-24 14:13 Maybe (untested), use crt and on the toplevel add a try finally around everything like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i try // main program code.finallyassign(output,'');rewrite(output);end; ---------------------------------------------------------------------- Fred vS · 2021-11-24 15:56 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Initialization{$ifdef debugcrt}  Assign(DebugFile,'debug.txt');  ReWrite(DebugFile);{$endif}  { Redirect the standard output }  assigncrt(Output);  Rewrite(Output);  TextRec(Output).Handle:=StdOutputHandle;  assigncrt(Input);  Reset(Input);  TextRec(Input).Handle:=StdInputHandle;{ Are we redirected to a file ? }  OutputRedir:= IsAtty(TextRec(Output).Handle)<>1;{ does the input come from another console or from a file? }  InputRedir :=   (IsAtty(TextRec(Input).Handle)<>1) or   (not OutputRedir and    (TTYName(TextRec(Input).Handle) <> TTYName(TextRec(Output).Handle)));{ Get Size of terminal and set WindMax to the window }  GetConsoleBuf;  WindMinX:=1;  WindMinY:=1;  WindMaxX:=ScreenWidth;  WindMaxY:=ScreenHeight;  WindMax:=((ScreenHeight-1) Shl 8)+(ScreenWidth-1);{Get Current X&Y or Reset to Home}  if OutputRedir then   begin     CurrX:=1;     CurrY:=1;   end  else   begin   { Set default Terminal Settings }     SetRawMode(True);   { Get current X,Y if not set already }     GetXY(CurrX,CurrY);     if (CurrX=0) then    begin        CurrX:=1;        CurrY:=1;        ttySendStr(#27'[H');    end;   {Reset Attribute (TextAttr=7 at startup)}      ttySendStr(#27'[m');    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 ---------------------------------------------------------------------- Fred vS · 2021-11-25 22:58 Quote from: marcov on November 24, 2021, 03:13:32 pmMaybe (untested), use crt and on the toplevel add a try finally around everything like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i try // main program code.finallyassign(output,'');rewrite(output);end;  OK, I did this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFPDLoop.DoRun;var  S: String;  b: boolean;begintry  Write('FPD ' + inttostr(StepNum) + ' ~> ');  ReadLn(S);  if trim(S) <> ''  then FLast := S;  if FLast <> '' then    begin    HandleCommand(FLast, b);    if b then inc(StepNum);    while b do      begin      GController.ProcessLoop;      GController.SendEvents(b);      end;    end; finallysystem.assign(output,'');rewrite(output);end; 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 ---------------------------------------------------------------------- Fred vS · 2021-11-26 14:48 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.  With this it is OK, the fpc error message are ignored (and in the file, the erorr is aligned !!!).  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program mytmessage_redirect;  uses     ...,  Classes, BaseUnix;     var      fs: TFileStream;      begin         fs := TFileStream.Create('error.log', fmOpenReadWrite or fmCreate);         FpDup2(fs.Handle, StdErrorHandle);          ...         Application.run;         fs.free;      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 ? ---------------------------------------------------------------------- Fred vS · 2021-11-28 04:07 Hum, lot of noise here!  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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Readln(S); use this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i S := mygetkeys(S); And here is the function mygetkeys used:   Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function mygetkeys(prompt: string) : string;const  CHARS = ['=', '+', '-', '.', '/', ':', '\' , '0'..'9', 'a'..'z', 'A'..'Z'];var  s : string = '';  stmp : string = '';  i : integer;  key : char = #00 ;  kw : word ;  k : TKeyEvent;begin   InitKeyBoard;  // important   while Key <>  #13 do // enter not pressed    begin          K:=GetKeyEvent;      K:=TranslateKeyEvent(K);      key := GetKeyEventChar(K);       case key of      #00 : begin               K:=GetKeyEvent;               K:=TranslateKeyEvent(K);                 kw := GetKeyEventCode(K);             case kw of                   65313 :  write ('UP'); // Up Arrow key pressed                 65315 :  write ('LEFT'); // Left Arrow key pressed                      65317 :  write ('RIGHT'); // right Arrow key pressed                 65319 :  write ('DOWN'); // down Arrow key pressed                end;             end;        #8 : begin // backspace            stmp := '';            for i := 1 to length(s) + length(prompt) do stmp := stmp + ' ';             Write(#13+ stmp);             s := system.copy(s,1, length(s) - 1);           Write(#13+ prompt + s);            end;               else        begin         if (key in CHARS) then         begin         S := s + char(key);            Write( key);         end;       end;             end;    end;    DoneKeyBoard; // important otherwise fpc message is not aligned.      result := s; end; Fre;D