Recent

Author Topic: read keys from windows konsole  (Read 776 times)

Matze

  • Newbie
  • Posts: 5
read keys from windows konsole
« on: February 07, 2025, 11:41:33 am »
Hello,
how can I read characters in FPC from Windows console with e.g.:
echo j | fpc.exe
If I start my program this way, it does not react to keyboard input (readkey / keypressed) any more.
What am I doing wrong?
Keys that were pressed before program started are also not read.

---
Lazarus 1.8.2
FPC 3.0.4
Windows 10

Bart

  • Hero Member
  • *****
  • Posts: 5538
    • Bart en Mariska's Webstek
Re: read keys from windows konsole
« Reply #1 on: February 07, 2025, 01:32:16 pm »
I assume this is a console program
I would assume reading from input should work, something like (untested):
Code: Pascal  [Select][+][-]
  1.   while not eof(input) do readln(somestring);

I did have some program that I coud pipe input into suing such a construct.

IIRC then this does not work if the program uses the crt unit, nor if the program has no console (Windows GUI app).

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 16653
  • Kallstadt seems a good place to evict Trump to.
Re: read keys from windows konsole
« Reply #2 on: February 07, 2025, 04:17:43 pm »
no need for crt.
just read/readln from system.
But I am sure they don't want the Trumps back...

Matze

  • Newbie
  • Posts: 5
Re: read keys from windows konsole
« Reply #3 on: February 07, 2025, 04:44:01 pm »
no, if i type
echo j | myprogram.exe
read / readln returns nothing and if then pressing a key is not recognized

Bart

  • Hero Member
  • *****
  • Posts: 5538
    • Bart en Mariska's Webstek
Re: read keys from windows konsole
« Reply #4 on: February 07, 2025, 05:28:39 pm »
This works for me:
Code: Pascal  [Select][+][-]
  1. program test;
  2. {$ifdef fpc}
  3. {$mode objfpc}
  4. {.$mode delphi}
  5. {$h-}
  6. //{$mode delphi}
  7. //{$modeswitch advancedrecords}
  8. {$endif}
  9. {$apptype console}
  10. //{$R+}
  11.  
  12. var
  13.   S: String;
  14.  
  15. begin
  16.   while not EOF do
  17.   begin
  18.     readln(S);
  19.     writeln('S=',S);
  20.   end;
  21. end.

Code: [Select]
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test < test.pas
S=program test;
S={$ifdef fpc}
S={$mode objfpc}
S={.$mode delphi}
S={$h-}
S=//{$mode delphi}
S=//{$modeswitch advancedrecords}
S={$endif}
S={$apptype console}
S=//{$R+}
S=
S=var
S=  S: String;
S=
S=begin
S=  while not EOF do
S=  begin
S=    readln(S);
S=    writeln('S=',S);
S=  end;
S=end.

C:\Users\Bart\LazarusProjecten\ConsoleProjecten>echo foobar | test
S=foobar

And this stops working as soon as you use the crt unit.
And AFAIK this also was the case in the good old TP days.

Bart

TRon

  • Hero Member
  • *****
  • Posts: 4153
Re: read keys from windows konsole
« Reply #5 on: February 07, 2025, 06:27:02 pm »
And AFAIK this also was the case in the good old TP days.
That is correct Bart.
Today is tomorrow's yesterday.

Matze

  • Newbie
  • Posts: 5
Re: read keys from windows konsole
« Reply #6 on: February 09, 2025, 03:43:42 pm »
ok
is there any way to get this work with unit crt?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12112
  • FPC developer.
Re: read keys from windows konsole
« Reply #7 on: February 09, 2025, 03:49:22 pm »
Try

Code: Pascal  [Select][+][-]
  1. reset(input,'');

as first statement in your program. Note that only preserves the output functionality, iskeypressed/readkey can't be used anymore.


 

TinyPortal © 2005-2018