'===========================================================================
' Subject: A FILTERED INKEY$ ROUTINE          Date: 11-05-96               
'  Author: Charlie Quante                     Code: QB, QBasic, PDS        
'  Origin: charlie@charlie.seanet.com       Packet: KEYBOARD.ABC
'===========================================================================
'FULL Header Extraction for this Source Code has been Enabled
' A sample program to demonstrate the TestKey subroutine.

DECLARE SUB TestKey (KeyFlag%, CaseFlag%, KeysAccepted$)

    CLS
    DEFINT A-Z
'----------------------------------------------------------------------------
'   Prompt the user for a number from 1 to 4.
'   call TestKey - CaseFlag is unimportant for this example.
'   Print the number of the item selected.

    PRINT "Select a number from 1 to 4: "
    TestKey KeyFlag, -1, "1234"
    PRINT "You selected item"; KeyFlag
'----------------------------------------------------------------------------  

'   Prompt the user for a letter from A to Z
'   Call TestKey - Note: Notice that you can have a combination of upper and
'                        lowercase letters.
'   In this example CaseFlag is set to -1 and any key pressed is converted
'   into uppercase. The same applies to the KeysAccepted$ string.
'   Convert the number returned by KeyFlag back into a letter.

    PRINT
    PRINT "Select a letter from A to Z: "
    TestKey KeyFlag, -1, "ABCDEfghijklmnopqrstuvwxyz"
    PRINT "You selected letter "; CHR$(34); CHR$(KeyFlag + 64); CHR$(34)
'----------------------------------------------------------------------------
  
'   Now CaseFlag is set to 0, and the user must enter either an uppercase
'   A through E, or a lowercase f through z

    PRINT
    PRINT "Okay, select another letter from A to Z: "
    TestKey KeyFlag, 0, "ABCDEfghijklmnopqrstuvwxyz"
    PRINT "You selected letter "; CHR$(34); CHR$(KeyFlag + 64); CHR$(34)

SUB TestKey (KeyFlag, CaseFlag, KeysAccepted$)
'Purpose: 
'           Get input from the user, and test it to see if it is one of
'           the keys that are acceptable.
'Variables:
'           CaseFlag      - If CaseFlag is -1 then all input, is converted
'                           to uppercase.
'           KeyFlag       - Returns a number indicating which acceptable key
'                           was pressed.
'           KeysAccepted$ - Holds the acceptable keys.
'           GetKey$ - Gets the key pressed via the INKEY$ command. 


    KeyFlag = 0
    IF CaseFlag THEN KeysAccepted$ = UCASE$(KeysAccepted$)

    DO WHILE KeyFlag = 0
        GetKey$ = ""
       
        DO WHILE GetKey$ = ""
            GetKey$ = INKEY$
        LOOP
       
        IF CaseFlag THEN GetKey$ = UCASE$(GetKey$)
        KeyFlag = INSTR(KeysAccepted$, GetKey$)
    LOOP
END SUB

