       ͻ
        Lesson 3 Part 070  F-PC 3.5 Tutorial by Jack Brown 
       ͼ


Further discussion of the KEY_TEST program from Lesson 3 Part 050

New Word:  KEY   Wait for user to press key on keyboard and
KEY  ( --   n )  return the key code n.  If n is < 128 then n
                 is the ASCII code for the key pressed.  If n
                 is > 128 then n is the function key scan code
                 + 128.

Old Word:  EXIT  Stops compilation when not in a colon definition
EXIT ( -- )      When compiled in a word, EXIT , will cause
                 termination of word execution when encountered.

:  KEY_TEST ( -- )
        BEGIN  CR  KEY       \  Wait for key press by user.
        DUP  CONTROL M  =    \  Control M is return key.
        IF DROP EXIT THEN    \  Exit infinite loop if pressed.
        DUP .  EMIT          \  Otherwise show key pressed.
        AGAIN ;

KEY_TEST <enter>
65 A                  <--- pressed upper case A
83 S                  <--- pressed upper case S
187  ??               <--- pressed F1 key
188  ??               <--- pressed F2 key

Note the ?? was one of the extended IBM characters and could not be
captured by Side Kick (TM).  As noted above the F-PC version of KEY
returns the 128 + keyboard scan code for any non ASCII keys that are
pressed.

ķ
 Problem 3.16  
Ľ
Use the word KEY_TEST to document the key codes returned for all non
ASCII keys on the IBM key board.  This means function keys, arrow keys,
HOME, END, etc.  Don't forget to try  Alt F1, Shift F1, Ctl F1, etc.

Another version of KEY...  or  We don't always like F-PC's  KEY so we
made our own version of KEY that we call  PCKEY .

Here is our PCKEY definition.  Our version returns a flag that can
be immediately tested to determine whether an ASCII key was pressed
(true flag returned) or a function key was pressed (false flag
returned).

Note:   BIOSKEY ( -- n) Is a primitive keyboard input word that returns
a 16-bit number whose high 8 bits consist of the keyboard scan code and
whose low 8-bits consist of the ASCII code of the key pressed.  If the
key is not an ASCII key then the low 8-bits are zero.

\ This is a different type of keyboard input routine.
\ Return  ASCII code and tf or  function key scan code and ff.
: PCKEY  ( --   n  flag )
       BIOSKEY DUP 127 AND    \ Mask of low 8-bits to check for ASCII
       IF   127 AND TRUE      \ Yes its ASCII, mask off ASCII value.
       ELSE FLIP    FALSE     \ Its a function key,  leave scan code.
       THEN ;

Here...  Watch and see what FLIP does....
HEX  MYQUIT
 Stack Empty. > AABB
 [1]   AABB > FLIP
 [1]   BBAA > FLIP

Stack picture:
FLIP  ( hilo -- lohi )  \ Flips hi 8-bits with low 8-bits.

And here is our word to test the new PCKEY

\ Test ascii keys and function keys.
:  PCKEY_TEST  ( -- )
        BEGIN  CR  PCKEY
        IF   DUP  CONTROL M  =         \  Control M is return key.
             IF   DROP EXIT THEN       \  Exit infinite loop if pressed.
             ." ASCII key character code = "
             DUP .  EMIT               \  Display code and character.
        ELSE ." Function  key  scan code = " .  \ Display scan code.
        THEN
        AGAIN ;

Sample execution:
DECIMAL PCKEY_TEST <enter>
ASCII key character code = 65 A
ASCII key character code = 83 S
Function  key  scan code = 59       <--- Pressed F1
Function  key  scan code = 60       <--- Pressed F2

Note: PCKEY returns the actual keyboard scan code for the function keys
as documented in the IBM Tech Reference manual.  F-PC's KEY returns the
actual scan codes + 128.

ķ
 Problem 3.17  
Ľ
Use the PCKEY_TEST program to document the actual scan codes for all the
non ASCII keys on an IBM PC keyboard.  Don't forget the key chords like
Alt F1, Ctl F1, Shift F1, etc.  Upload your list here for the rest of
us.

Ŀ
  Please move to Lesson 3 Part 080  

