I need help with Case Statements and Readkey. 
Author Message
 I need help with Case Statements and Readkey.

I'm trying to make a menu program which displays some choices inside a box
and when you press up the bar moves up and when you press down the bar
moves down. (thrilling stuff:) Anyway its gone fine until I get up to the
reading of the keys and the moving of the bar. Below I've got code from
this part of the program. The 'Choice := 1;' is so that I know that the bar
is currently in position one ie. starting position. I've been told that the
code for the enter key is 13 so the 'while read <> '13' do' is meant to
continue the moving of the bar until the user presses enter. I was also
told when you press up the code is '0' + 'P' and when down '0' + 'H' which
explains the rest of the code. MoveBarDown, MoveBarUp and EnteProcedure are
all procedures in the same unit as this one and there dont seem to be any
problems with them; mostly because I cant use them yet because when I run
the program It just displays the box with the first choice highlighted with
the bar and doesn't do anything no matter what I press. Not even Break
works. All the codes that I have used may be wring or the structure of the
Case statements or anything else so could someone Pleas help me?

Choice := 1;
while read <> '13' do
begin
  read := readkey;
  case read of
       '0' : case read of
                  'P': MoveBarDown(choice,barcolor,box);
                  'H': MoveBarUp(choice,barcolor,box);
                  else then end;
       '13': EnterProcedure(choice);
       else then end;
end;

Thanks, by the way this isnt the entire procedure either just part of it:)

JLG



Tue, 30 Nov 1999 03:00:00 GMT  
 I need help with Case Statements and Readkey.

Quote:

[snip]

> Choice := 1;
> while read <> '13' do
> begin
>   read := readkey;
>   case read of
>        '0' : case read of
>                   'P': MoveBarDown(choice,barcolor,box);
>                   'H': MoveBarUp(choice,barcolor,box);
>                   else then end;
>        '13': EnterProcedure(choice);
>        else then end;
> end;

> Thanks, by the way this isnt the entire procedure either just part of it:)

> JLG

You need to change you case statement to be:
  CASE Read OF
   #0  : BEGIN
          Read := Readkey; {need second readkey to read extended char}
          CASE Read OF
           'P' : ..
           'H' : ..
           ELSE Write(^G); {beep}
          END;
         END;
   #13 : ...
  END;

Note the use of the #<char code> notation rather than the 'char'.  Also,
you
shouldn't use 'Read' as a variable because that is a standard function
name
for Pascal.

--
---- Michael Slack ----



Tue, 30 Nov 1999 03:00:00 GMT  
 I need help with Case Statements and Readkey.


writes
<snip>

Quote:
>Choice := 1;
>while read <> '13' do
>begin
>  read := readkey;
>  case read of
>       '0' : case read of
>                  'P': MoveBarDown(choice,barcolor,box);
>                  'H': MoveBarUp(choice,barcolor,box);
>                  else then end;
>       '13': EnterProcedure(choice);
>       else then end;
>end;

>Thanks, by the way this isnt the entire procedure either just part of it:)

>JLG

As the code returned by readkey is an ascii code rather than a string,
if you change '13' and '0' to #13 and #0 your procedure will work fine

Hope this helps

--

In principle,   is there uncertainty that
Heisenberg was working his best in chaos?
=========================================



Tue, 30 Nov 1999 03:00:00 GMT  
 I need help with Case Statements and Readkey.

Quote:
> > Choice := 1;
> > while read <> '13' do
> > begin
> >   read := readkey;
> >   case read of
> >        '0' : case read of
> >                   'P': MoveBarDown(choice,barcolor,box);
> >                   'H': MoveBarUp(choice,barcolor,box);
> >                   else then end;
> >        '13': EnterProcedure(choice);
> >        else then end;
> > end;

> > Thanks, by the way this isnt the entire procedure either just part of it:)
> You need to change you case statement to be:
>   CASE Read OF
>    #0  : BEGIN
>           Read := Readkey; {need second readkey to read extended char}
>           CASE Read OF
>            'P' : ..
>            'H' : ..
>            ELSE Write(^G); {beep}
>           END;
>          END;
>    #13 : ...
>   END;

> Note the use of the #<char code> notation rather than the 'char'.  Also,
> you
> shouldn't use 'Read' as a variable because that is a standard function
> name for pascal.

   That's not right, either.  It won't work at all, because your literals
are all wrong.  Specifically, '0' isn't the value which you get for an
extended/special key (the cursor keys, etc.), so this logic won't do what
you intend.
   I suggest you take a wholly different approach to declaring the
literals - one which uses the ASCII values you're actually using.  Like
this:

Var C1,C2 : char;
  C1 := ReadKey;  { fetch a character from keyboard }
  case C1 of
   #00 : begin               { special/extended key }
           C2 := ReadKey;         { fetch scan code }
           case C2 of
            #71 : process_Home_Key;
            #72 : process_UpArrow_Key;
            #73 : process_Pg_Up_Kay;
            etc...
           end;
    else  begin                        { normal key }
          end
  end

   Even this code is somewhat overdone, but I did so to show the whole
concept of 2 key reads, via ReadKey - the #00 case could use the direct
"case ReadKey of" way of obtaining the scan code, if you like.  The point
is, use the #nn notation to show the ASCII values, and you'll avoid the
(embarrassing) problem of using/posting code which "looks" okay but
isn't...  8<}}



Thu, 02 Dec 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. CASE statement help needed

2. HELP!! Using CASE statement with a SET

3. Case Statement Help

4. I need help fixing this CASE Select code, please help

5. using an REAL as part of a CASE statement

6. problem with my case statement

7. Case Statement

8. Case Statement

9. case statement efficiency

10. Checksum function? (was: Re: CASE statement problems)

11. Checksum function? (was: Re: CASE statement problems)

12. Using strings in CASE statements

 

 
Powered by phpBB® Forum Software