[fpc-pascal] Writeln() behaves differently on Windows and Linux, why?

Tomas Hajny XHajT03 at hajny.biz
Fri Jul 10 01:04:06 CEST 2020


On 2020-07-10 00:40, Bo Berglund via fpc-pascal wrote:
> On Thu, 09 Jul 2020 23:11:34 +0200, Tomas Hajny
> <XHajT03 at hajny.biz> wrote:
> 
>> You can use the unit Keyboard as well, which is cross-platform.
> 
> I did as follows:
> 
> uses
>   ...
>   Keyboard,
>   ...
> begin
>   ...
>     repeat
>       //ch := ReadKey; //ReadKey is not supported by Keyboard...
>       Read(ch);
>     until ch='q'; {q}
>   ...
> end.

You added unit Keyboard to the uses clause, but you don't use it. ;-)


> Now what happened is that the display is back to normal without these
> missing carriage returns.
> 
> But I also must hit Enter after using the q key in order for the loop
> to break.
> 
> So Crt was the culprit for the messed up Writeln() output, but
> replacing it with Keyboard brought back the old keyboard read
> behaviour.
> 
> Removing Keyboard and Crt from uses does not change anything, but the
> display now is sensible at least. So Keyboard is not needed it seems.
> At least it does not add anything in this program.
> 
> So I will have to live with q<Enter> to break the loop...

Not necessarily.

----
uses
  Keyboard;

var
  K: TKeyEvent;

begin
  InitKeyboard;
  while char (TKeyRecord (K).KeyCode) <> 'q' do
   begin
    WriteLn (char (TKeyRecord (K).KeyCode));
    K := GetKeyEvent;
   end;
  DoneKeyboard;
end.
=========

The above is just a dirty hack without handling the pressed keys 
properly, but it hopefully shows the basics.

Tomas


More information about the fpc-pascal mailing list