I’m writing a utility program to do something useful, stay tuned, and in doing so I needed a routine to get a yes or no response from the user. I didn’t want the user to have to press the RETURN key, so I decided to read the keyboard without an input. I also wanted validation on the input so only valid keys would exit, in this case Y,y,N,n.
I created the following routine which returns a BYTE value. The value will be 1 if either Y is pressed. The value will be 0 if either N is pressed. It takes one parameter which is a BYTE value. 1 indicates the routine should echo Y or N to the display after the press. The source will not be broken down as it is commented well.
Routine Source
; Func..: WaitYN(BYTE bEcho) ; Param.: bEcho=1 to print Y or N ; Return: BYTE (1=Y,0=N) ; Desc..: Waits for Y or N keypress BYTE FUNC WaitYN(BYTE bEcho) ; Declare bRKEYCH to point to mem location 764 BYTE bRKEYCH=764,bRet=[0],bKey=[0] DO ; Wait for any keypress ; Continual loop while no keys pressed WHILE bRKEYCH=KNONE DO OD ; Get keypress and reset debounce bKey=bRKEYCH bRKEYCH=KNONE ; Stay in loop until YyNn UNTIL bKey=43 OR bKey=107 OR bKey=35 OR bKey=99 OD ; If Yy then set return 1 if bKey=43 OR bKey=107 then bRet=1 fi ; If echo on if bEcho=1 then ; If Y, print Y if bRet=1 then Put('Y) ; Else print N else Put('N) fi fi RETURN(bRet)
Program Source
MODULE ; Define the value of no key pressed DEFINE KNONE = "255" ; Func..: WaitYN(BYTE bEcho) ; Param.: bEcho=1 to print Y or N ; Return: BYTE (1=Y,0=N) ; Desc..: Waits for Y or N keypress BYTE FUNC WaitYN(BYTE bEcho) ; Declare bRKEYCH to point to mem location 764 BYTE bRKEYCH=764,bRet=[0],bKey=[0] DO ; Wait for any keypress ; Continual loop while no keys pressed WHILE bRKEYCH=KNONE DO OD ; Get keypress and reset debounce bKey=bRKEYCH bRKEYCH=KNONE ; Stay in loop until YyNn UNTIL bKey=43 OR bKey=107 OR bKey=35 OR bKey=99 OD ; If Yy then set return 1 if bKey=43 OR bKey=107 then bRet=1 fi ; If echo on if bEcho=1 then ; If Y, print Y if bRet=1 then Put('Y) ; Else print N else Put('N) fi fi RETURN(bRet) ; Main routine PROC Main() ; Declare a byte to store the response BYTE bY=[0] ; Ask a question, get a YN from user Print("Save (Y/N)?") bY=WaitYN(1) ; If response was Y, print YES if bY=1 then PrintE(" (YES)") ; Otherwise print NO else PrintE(" (NO)") fi RETURN
Results
My example program running over and over asking the same question. I answered with a mix of upper and lower case Y and N: