Example of Buffered Keyboard Input Using Basic

ID: Q42949


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for MS-DOS, version 1.0
  • Microsoft QuickBASIC for MS-DOS, versions 2.0, 2.01, 3.0, 4.0, 4.0b, 4.5
  • Microsoft BASIC Compiler for MS-DOS and MS OS/2, versions 6.0, 6.0b
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2, version 7.0


SUMMARY

The following is a program example of buffered keyboard input with Microsoft Visual Basic for MS-DOS. This example also works with Microsoft Quick Basic for MS-DOS, Microsoft Basic Compiler for MS-DOS and MS OS/2, and Microsoft Basic PDS for MS-DOS and MS OS/2.

Buffered input allows input from the screen, but limits the total number of characters that can be input and typed. It is a standard form of input for business applications. The Microsoft Basic products mentioned above do not offer this as a built-in form of input. It must be programmed with a combination of other Basic statements and functions.

Note that Basic does allow input into a fixed-length string, but will read only up to the maximum length for that string. However, this does not prevent you from typing any number of characters, which is undesirable when doing formatted screen input. This example shows only one way to do this. An added feature would be the addition of a cursor.

Code Example


DECLARE SUB BufInput (StringVar$, Limit%)
'NOTE: The DECLARE statement is not supported under Microsoft
'      Quick Basic version 3.0 or earlier, and can be removed.

CLS
Limit% = 10 ' Number of characters to input.

' Prompt for the input string.
LOCATE 10, 5
PRINT "PLEASE INPUT A STRING OF TEN CHARACTERS: ";
COLOR 0, 7: PRINT SPACE$(Limit%)
LOCATE 10, 46

' Input a buffered string.
CALL BufInput(a$, Limit%)

COLOR 7, 0
CLS

' Print out the result.
LOCATE 10, 10
PRINT "THE STRING YOU INPUT WAS: ";
COLOR 0, 7
PRINT a$
COLOR 7, 0

END

SUB BufInput (StringVar$, Limit%)
BackSp$ = CHR$(8)
Enter$ = CHR$(13)
Length% = 0

' Loop waiting for input. ENTER terminates the routine.
WHILE char$ <> Enter$
char$ = INKEY$
   IF char$ <> "" THEN
     IF (Length% < Limit%) OR (char$ = BackSp$) THEN
        SELECT CASE char$
        CASE " " TO "~"  ' Input printable characters only.
           StringVar$ = StringVar$ + char$
           PRINT char$;
           Length% = Length% + 1
        CASE BackSp$  ' If BackSpace is pressed, erase.
          IF Length% <> 0 THEN
             Length% = Length% - 1
             StringVar$ = LEFT$(StringVar$, Length%)
             CurrX% = CSRLIN
             CurrY% = POS(0) - 1
             LOCATE CurrX%, CurrY%
             PRINT " "
             LOCATE CurrX%, CurrY%
          END IF
        END SELECT
     ELSE
        PLAY "o1AL32"
     END IF
   END IF
WEND

END SUB 

Additional query words: VBmsdos QuickBas BasicCom 1.00 2.00 2.01 3.00 4.00 4.00b 4.50 6.00 6.00b 7.00

Keywords :
Version : MS-DOS:1.0,2.0,2.01,3.0,4.0,4.0b,4.5; :6.0,6.0b,7.0
Platform : MS-DOS
Issue type :


Last Reviewed: December 8, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.