Recent

Author Topic: Keboard Input in Free Pascal under Windows 10  (Read 3020 times)

Weosule

  • New member
  • *
  • Posts: 5
Keboard Input in Free Pascal under Windows 10
« on: February 16, 2019, 01:58:35 am »
 :(

Hello Everyone....

I have been using Turbo Pascal up to version 7 for DOS and Borland Delphi 7 up until the advent of Windows 7. I have recently started using Free Pascal and although I have am glad to have written some new programs in FP and recompile some programs I wrote using Turbo Pascal 7, I have been having issues recompiling and using programs that use Graphic rather than Text format. They compile but I find I have to switch to a second window to enter anything from the keyboard. It may be a noobie error or I have made a stooopid but I hope you'll not be angry with me and send a squad of trolls to my house to beat me up and skin my pet ferrit.


I have been using the following procedure to read from the keyboard and write the input to a graphic screen which worked fine up to Turbo Pascal 7. However, I did not write this code and don't know how to amend it for it to work under FP. I hope someone would be kind enough to revise it for me.


Thank you all in advance.

Weosule
Code: Pascal  [Select]
  1. PROCEDURE ReadlnXY(X, Y: Integer; VAR S: String);
  2.  
  3. {$V-} {Relax string checking}
  4. VAR
  5.   Ch: Char;         { key from keyboard }
  6.   Done: boolean;    { our flag for quiting }
  7.   CurColor: word;   { color to write text in }
  8.   OldX: Integer;    { old x }
  9.  
  10. BEGIN
  11.   S := '';
  12.   CurColor := GetColor;
  13.   MoveTo(X, Y);
  14.   Done := False;
  15.   WHILE NOT Done  DO
  16.     BEGIN
  17.       Ch := Readkey;  { get a single key }
  18.       CASE Ch of
  19.         #0: { extra key - two chars - let's ignore them }
  20.             Ch := Readkey;
  21.         #13: { return key }
  22.              Done := true; { we got our string, let's go }
  23.         #32..#126:  { ASCII 32 (space) through 126 (tilde) }
  24.              BEGIN
  25.                OutText(Ch);
  26.                S := Concat(S, Ch);
  27.              END;
  28.         #8: IF Length(S) > 0 THEN
  29.               BEGIN
  30.                 { move back to last character }
  31.                 OldX := GetX - TextHeight(S[Length(S)]);
  32.                 MoveTo(OldX, GetY);
  33.                 { over write last character }
  34.                 SetColor(0);
  35.                 OutText(S[Length(S)]);
  36.                 SetColor(CurColor);
  37.                 MoveTo(OldX, GetY);
  38.                 { remove last character from the string }
  39.                 Delete(S, Length(S), 1);
  40.               END;
  41.             END;
  42.       END;
  43.  
  44. END; { ReadlnXY }
  45.  

benohb

  • Full Member
  • ***
  • Posts: 184
Re: Keboard Input in Free Pascal under Windows 10
« Reply #1 on: February 16, 2019, 10:23:41 am »

What do you mean "Graphic" .   ..
 This ?? https://www.freepascal.org/docs-html-3.0.0/rtl/graph/index.html
Can you upload an image for 2 windows  (screenshot)
Quote
They compile but I find I have to switch to a second window to enter anything from the keyboard

Nitorami

  • Sr. Member
  • ****
  • Posts: 368
Re: Keboard Input in Free Pascal under Windows 10
« Reply #2 on: February 16, 2019, 12:07:03 pm »
He is talking about the ancient graph and crt units.

The procedure does work, see the compilable code below. However, when entering the characters, you need to bring the console window into focus, not the graph window, otherwise your keyboard input goes nowhere.


Code: Pascal  [Select]
  1. uses graph, crt;
  2. var col,Driver,mode: smallint;
  3.  
  4.     PROCEDURE ReadlnXY(X, Y: Integer; VAR S: String);
  5.  
  6.     {$V-} {Relax string checking}
  7.     VAR
  8.       Ch: Char;         { key from keyboard }
  9.       Done: boolean;    { our flag for quiting }
  10.       CurColor: word;   { color to write text in }
  11.       OldX: Integer;    { old x }
  12.  
  13.     BEGIN
  14.       S := '';
  15.       CurColor := GetColor;
  16.       MoveTo(X, Y);
  17.       Done := False;
  18.       WHILE NOT Done  DO
  19.         BEGIN
  20.           Ch := Readkey;  { get a single key }
  21.           CASE Ch of
  22.             #0: { extra key - two chars - let's ignore them }
  23.                 Ch := Readkey;
  24.             #13: { return key }
  25.                  Done := true; { we got our string, let's go }
  26.             #32..#126:  { ASCII 32 (space) through 126 (tilde) }
  27.                  BEGIN
  28.                    OutText(Ch);
  29.                    S := Concat(S, Ch);
  30.                  END;
  31.             #8: IF Length(S) > 0 THEN
  32.                   BEGIN
  33.                     { move back to last character }
  34.                     OldX := GetX - TextHeight(S[Length(S)]);
  35.                     MoveTo(OldX, GetY);
  36.                     { over write last character }
  37.                     SetColor(0);
  38.                     OutText(S[Length(S)]);
  39.                     SetColor(CurColor);
  40.                     MoveTo(OldX, GetY);
  41.                     { remove last character from the string }
  42.                     Delete(S, Length(S), 1);
  43.                   END;
  44.                 END;
  45.           END;
  46.  
  47.     END; { ReadlnXY }
  48.  
  49. var s: string;
  50. begin
  51.   Driver := D8bit; mode := m800x600;
  52.   InitGraph (Driver,mode,'');
  53.   readlnxy (100,100,s);
  54.   readln;
  55. end.

Bart

  • Hero Member
  • *****
  • Posts: 3546
    • Bart en Mariska's Webstek
Re: Keboard Input in Free Pascal under Windows 10
« Reply #3 on: February 16, 2019, 03:50:59 pm »
IIRC then it is not advised to use crt and graph unit together.

Bart

Weosule

  • New member
  • *
  • Posts: 5
Re: Keboard Input in Free Pascal under Windows 10
« Reply #4 on: February 25, 2019, 02:12:32 am »
It seems strange to me that if there is an graphic output function, there is not an input function. :o

Bart

  • Hero Member
  • *****
  • Posts: 3546
    • Bart en Mariska's Webstek
Re: Keboard Input in Free Pascal under Windows 10
« Reply #5 on: February 25, 2019, 11:51:59 am »
If your program needs graphical interface, why not write a modern GUI version of your program using Lazarus.
All you need to redesing is the input basically, for which you use a TEdit.

There is nothing you can't do using Lazarus that you can do uing the Graph unit (except maybe for turtle graphics?).

Bart