Turbo Pascal 7 Utility program construction

Hi!
My Name's Chris Berry.
I am currently on an 'A' level computing course. I want to use the arrow keys in my validation peocedure for my project. I know the ascii codes for the arrows but can't get the codeing right. Please can you help........Merry Christmas and a Happy New Year...........Chris.
chrisbeeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HypoCommented:
Hi,
Here's the procedure I use for testing keys... (Getkey).

Program TestKeys;

uses crt;

var Ext : Boolean;
    Key : Byte;

Procedure GetKey;
begin
 Key := ord(Readkey);
 If Key = 0 then begin
  Ext := True;
  Key := ord(Readkey);
 end else Ext := false;
end;


begin
  clrscr;
  Writeln('Key test example - Press ESC to quit');
  repeat
   GetKey;
   Write('Key = ',key:3,'  Extended = ',ext:5);
   If Ext = false then begin
    Writeln;
   end else begin
    if Key = 80 then Write('   : Down arrow');
    if Key = 72 then Write('   : Up arrow');
    if Key = 75 then Write('   : Left arrow');
    if Key = 77 then Write('   : Right arrow');
    Writeln;
   end;
  until (Key = 27) and not ext;
end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HypoCommented:
Some keys on the keyboard are extended keys. they have the same scan code as another key... for instance, Up arrow has the same Scancode as "H" key and Downarrow has the same as "P".

When the keyboard sends the scancode of an extended key to the computer, it first sends a zero followed by the extended key's scancode.

If it's not an extended key it only sends the scancode without the preceeding zero.

{

 If ScanCode = 0 then
 KeyIsExtended and ScanCode <= next value from keyboard
 
}

So when you read from the keyboard in pascal, using readkey... and someone presses an extended key,  the first readkey will return zero and the second one will return the extended key's scancode.


0
chrisbeeAuthor Commented:
Tp Hypo

Thank you for the clear explanation on my question I can see the format behind it now but being new to programming it takes me a while to grasp it. I am satisfied with your answer but have yet to put into practice. I shall try the code out you have presented then if successful transfer it to my project integrating into it the values Iam using. Thank you once again and a happy New Year to you.  Chrisbee.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

chrisbeeAuthor Commented:
To Hypo!
Further to my question regarding the use of the 'arrow keys' in my Pascal project.
You kindly gave me the code to capture the keys which I tested outside my prog. However, applying them to my program to allow a user to access their inputed data there was no response. I used a series of nested 'IF'statements applying x and y coordinates.
should I apply this as a separate question or can you advise.

Thank you Chrisbee.
0
HypoCommented:
You don't need to make another question for this... we can solve this here...

I'm not actually sure what you mean with no response, plz explain:

If I got you right, then this code might be usefull as an example.

Program Menu;

uses crt;

var  Key : byte;
     Ext : boolean;
     Lne : word;

Procedure GetKey;
begin
 Key := ord(readkey);
 if Key = 0 then begin
  Key := ord(readkey);
  Ext := true;
 end else Ext := false;
end;

begin
  clrscr;
  writeln('->First option - ');
  writeln('  Secnd option - ');
  writeln('  Third option - ');
  writeln('  Forth option - ');
  writeln('  Fifth option - ');
  Lne := 0;
  GotoXy(3,1);
  repeat
   If Lo(Lne) <> Hi(Lne) then begin
    GotoXY(1,(Hi(Lne)) mod 5+1);
    write('  ');
    GotoXY(1,(Lo(Lne)) mod 5+1);
    write('->');
    Lne := Lne and $FF + Lne SHL 8;
   end;
   if keypressed then GetKey else Key := 0;
   If Ext then begin
    If (Key = 80) then Inc(Lne);
    If (Key = 72) then dec(Lne);
   end;
  until (Key = 27) and not ext;
end.
0
chrisbeeAuthor Commented:
Hi Hypo!

Thank you for your prompt response to my comment. I will put the ideas you have given me against mine to see where I have gone wrong.

Best wishes and thanks........Chrisbee.
0
chrisbeeAuthor Commented:
To Hypo!

In response to your second comments  with my arrows question you asked me to explain. This is what I want to use the 'arrows' on the keyboard for:

In my project there are procedures which ask the user to input data from the keyboard. ie;
NAME:
ADDRESS 1:
ADDRESS 2;  ect;
 If they arrive at the bottom line and find they have left an error in the entry in the first line I want to offer them a 'localised edit facility' by using the arrow keys to go back, delete and  correct the error.
I have written the 'delete character'  (using the ascii code for the backspace key) delete procedure which deletes a character from a substring allowing them to edit the line they are on.
 I want to move within the entered text to make corrections. I use the arrow keys a lot so take them for granted.

with best wishes.......Chrisbee.
0
HypoCommented:
I understand your problem now,
(as usual) I've made a pice of code for you. The Function you need is called "EditString" in my example below, The code is a bit messy (just wrote it).

Anyway, run the program and use arrow keys to select a line, then press enter to edit that line. (You can use Delete/Insert/Home/End keys)

This is how you use the EditString function: EditString(StringYouWantToEdit: string,
XPositionForStringOnScreen: word,
YPositionForStringOnScreen: word,
MaxLengthOfString: word) : byte;

The Function returns a byte value. If that value is:
0 then string was terminated by Enter/Return
1 then Tab was pressed
2 then UpArrow was pressed
3 then DownArrow was perssed
4 then Escape was pressed.

enjoy!

Program EditStrings;
uses crt;

type TTColor = record
  Active,
  Inactive : word;
end;

Const TColor : TTColor = (Active:10; Inactive:10);

var Tmp : array[0..3] of string;
    Lp  : word;
    Ec  : byte;

    Key : byte;
    Ext : boolean;

Procedure GetKey;
begin
  Key := ord(readkey);
  if Key = 0 then begin
   Key := ord(readkey);
   Ext := true;
  end else Ext := false;
end;


Function EditString(var S : string; Wx,Wy,MaxLen : word) : byte;
var
    Pos,
    ECode  : byte;
    Insrt  : Boolean;
    T      : string;

Procedure ClearLine(X,Y,Len : word; Col : byte); assembler;
asm
 MOV AX,SegB800
 MOV ES,AX
 MOV AX,Y
 MOV DI,Y
 SHL AX,4
 SHL DI,6
 ADD DI,AX
 ADD DI,X
 ADD DI,DI
 MOV AH,Col
 MOV CX,Len
 MOV AL,32
 REP STOSW
end;

procedure NormalCursor; assembler;
asm
 MOV AH,01;
 MOV CH,4;
 MOV CL,5;
 INT 10h;
end;

procedure InsertCursor; assembler;
asm
 MOV AH,01;
 MOV CH,0;
 MOV CL,8;
 INT 10h;
end;

begin
 Insrt := False;
 T := S;
 TextColor(Lo(TColor.Active));
 TextBackground(Hi(TColor.Active));
 Pos := Length(T);
 repeat

  GotoXy(Wx,Wy);   { Update string on Screen }
  Write(T);
  ClearLine(Wx+Length(T)-1,Wy-1,MaxLen-Length(T),Lo(TColor.Active)+Hi(TColor.Active) shl 4);
  GotoXy(Wx+Pos,Wy);

  GetKey;          { Check for and handle Keypress }
  If Ext then begin
   If (Key = 71) then Pos := 0;
   If (Key = 79) then Pos := Length(T);
   If (Key = 77) and (Pos < Length(T)) then inc(Pos);
   If (Key = 75) and (Pos > 0) then dec(Pos);
   If (Key = 83) then delete(T,Pos+1,1);
   If (Key = 82) then begin
    Insrt := not Insrt;
    If Insrt then InsertCursor else NormalCursor;
   end;
  end else begin
   If (Key = 8) and (Pos > 0) then begin delete(T,Pos,1); dec(Pos); end;
   If (Key > 31) then begin
    If Insrt and (Pos < Length(T)) then begin
     T[Pos+1] := chr(Key);
     inc(Pos);
    end else if Length(T) < MaxLen then begin
     insert(chr(Key),T,Pos+1);
     inc(Pos);
    end;
   end;
  end;
 { Exit loop if keypress = Escap, Enter, Tab, Up arrow or Down arrow }
 until (Key = 27) or (Key = 13) or (Key = 9)
        or (Ext and ((Key = 72) or (Key = 80)));

 If Key = 27 then begin T := S; ECode := 4; end else
 If Key = 13 then Ecode := 0 else
 If Key =  9 then Ecode := 1 else
 If Key = 72 then Ecode := 2 else
 If Key = 80 then Ecode := 3;

 TextColor(Lo(TColor.InActive));
 TextBackground(Hi(TColor.InActive));
 GotoXy(Wx,Wy);
 Write(T);
 ClearLine(Wx+Length(T)-1,Wy-1,MaxLen-Length(T),Lo(TColor.InActive)+Hi(TColor.InActive) shl 4);
 If Insrt then NormalCursor;

 EditString := ECode;
 S := T;
end;

begin
 clrscr;
 TColor.Active   := 15+1 shl 8; { Set Active/Inactive textcolors }
 TColor.InActive :=  7;
 TextColor(TColor.InActive);
 TextBackground(Hi(TColor.InActive));

 Tmp[0] := 'Line #1';
 Tmp[1] := 'Line #2';
 Tmp[2] := 'Line #3';
 Tmp[3] := 'Line #4';

 For lp := 3 downto 0 do begin
  GotoXY(10,4+Lp+Lp);
  Writeln(Tmp[lp]);
 end;
 GotoXY(10,4);

 repeat
  GetKey;
  If Ext then begin
   If Key = 72 then dec(lp);
   If Key = 80 then inc(lp);
   Lp := Lp and 3;
   GotoXy(10,4+Lp+Lp);
  end else begin
   If Key = 13 then repeat
    Ec := EditString(Tmp[Lp],10,4+Lp+Lp,50);
    If (Ec = 1) or (Ec = 3) then inc(Lp);
    If (Ec = 2) then dec(Lp);
    Lp := Lp and 3;
   until (Ec = 0) or (Ec = 4);
  end;
 until (Key = 27);
 Textbackground(0);
end.
0
chrisbeeAuthor Commented:
To Hypo!
Thanks again for your help I feel quite mean as to the points I gave you for this as the code you given me is very complex. I will study this closely and apply it to my project but I have a lot of reading to do to write functions and procedures like that off the back of my head.

I shall watch out for your name as I have another problem later in my project which involves 'graphics' I shall be more generous with my points then.
best wishes and a happy new year
...............Chrisbee...............
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.