home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / MADTRB13.ZIP / KBD.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-05-26  |  3.7 KB  |  107 lines

  1. {  TURBO PASCAL
  2. Version 3.0
  3. Reading from the KBD device
  4.  
  5. ===================
  6. This program demonstrates the behavior of the keyboard buffer
  7. when reading string variables:  }
  8.  
  9. program KBDdemo;
  10.  
  11. type
  12.   MaxString = string[10];
  13. var
  14.   s1, s2 : MaxString;
  15.  
  16. begin
  17.   ReWrite(KBD);                { 1.  Clear the KBD buffer     }
  18.   s1 := 'one';                 { 2.  Initialize s1            }
  19.   s2 := 'two';                 { 3.  Initialize s2            }
  20.   Read(KBD, s1);               { 4.  Read the first string    }
  21.   Read(KBD, s2);               { 5.  Read the second string   }
  22.   Writeln('1: ', s1:5);        { 6.  Echo the value of  s1    }
  23.   Writeln('2: ', s2:5);        { 7.  Echo the value of  s1    }
  24. end.
  25.  
  26.  
  27.  
  28.   Analysis of the Program
  29.  
  30.   Line#  Explanation
  31.   -----  -----------
  32.   1,2,3  Initialize the KBD buffer and both string variables.
  33.  
  34.   4      Type: 1234 and then hit the <ENTER> key.   The Read
  35.          procedure keeps reading until it "looks ahead" and finds
  36.          a carriage return (denoted as ^M).  Note that it does
  37.          not actually "read" the ^M.  Instead, it reads all charac-
  38.          ters until it "looks ahead" and sees an EOF character (^Z)
  39.          or a ^M.  At that time, the Read procedure terminates and
  40.          the NEXT character to be read is the ^M.
  41.  
  42.   5      Now the Read procedure "looks ahead" for a ^M and -- it
  43.          finds one!  It returns a null string value because no
  44.          characters were detected before it found the ^M.
  45.  
  46.   6      s1 has the value that was input:  1234.
  47.   7      s2 has a null value.
  48.  
  49.  
  50.   Suggestions
  51.   -----------
  52.   First, make this program behave as expected by inserting the following
  53.   statement between lines 4 and 5:
  54.  
  55.      ReWrite(KBD);                { 4.5  Clear the KBD buffer    }
  56.  
  57.   This statement clears the KBD buffer and the program will behave quite
  58.   predictably.
  59.  
  60.   Note that the KBD buffer is not buffered and probably should not be
  61.   used for reading strings.  The advantages of reading from the KBD are:
  62.  
  63.     1.  A keystroke can be detected even if no <CR> was entered.
  64.     2.  Any keyboard key can be detected and processed -- including
  65.         function keys, arrow keys, etc.
  66.     3.  Keystrokes are not automatically echoed to the console; this
  67.         enables the program to filter all keyboard input and control
  68.         console output (useful for entering passwords).
  69.  
  70.   The best way to use the KBD device is to read character variables only:
  71.  
  72.     program KBDdemo;
  73.  
  74.     type
  75.       MaxString = string[10];
  76.  
  77.     var
  78.       s1, s2 : MaxString;
  79.  
  80.     procedure StringRead(var s : MaxString);
  81.     var
  82.       ch  : char;
  83.     begin
  84.       s := '';    { Initialize the string                        }
  85.       repeat
  86.         Read(KBD, ch);
  87.         case ch of
  88.           ' ' .. '~' : begin
  89.                          s := s + ch; { add character to string   }
  90.                          { To echo this character to the console,
  91.                            insert: Write(ch);  I omit this statement
  92.                            here to make this routine behave exactly
  93.                            as if the string was being read from the
  94.                            KBD device }
  95.                        end; (* legal characters *)
  96.         end; (* case *)
  97.        until ch in [^M, ^Z];       { Quit if EOF or <CR> is entered  }
  98.     end; (* StringRead *)
  99.  
  100.     begin
  101.       ReWrite(KBD);                { 1.  Clear the KBD buffer     }
  102.       StringRead(s1);              { 2.  Read the first string    }
  103.       StringRead(s2);              { 3.  Read the second string   }
  104.       Writeln('1: ', s1:5);        { 6.  Echo the value of  s1    }
  105.       Writeln('2: ', s2:5);        { 7.  Echo the value of  s2    }
  106.     end.
  107.