home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol291 / kbd.dqc / kbd.doc
Encoding:
Text File  |  1986-12-17  |  4.3 KB  |  123 lines

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