Basic Program to Read Characters from the Screen into a String (43527)



The information in this article applies to:

  • Microsoft Visual Basic for MS-DOS
  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System for MS-DOS 7.0
  • Microsoft Basic Professional Development System for MS-DOS 7.1

This article was previously published under Q43527

SUMMARY

The program below demonstrates how to use the PEEK statement to read text that is printed on the screen into a Basic string variable. The program PEEKs video memory (at segment B000h) to read the characters from the screen.

(Note: An easier method to read characters from the screen is to use the SCREEN function, which reads a character's ASCII value or its color from a specified screen location.)

MORE INFORMATION

For a color display, you must change the address of the DEF SEG statement in the function from B000h to B800h. Note that any attributes (blinking, highlight, and so on) will be lost when the text is put into the string.

Example Program

' To try this example in VBDOS.EXE:
' 1. From the File menu, choose New Project.
' 2. Copy the code example to the Code window.
' 3. Press F5 to run the program.

DECLARE FUNCTION readstr$ (xline%, xstart%, xstop%)
CLS
PRINT "Now is the time"         ' Put some text on the screen.
PRINT "for all good men"
PRINT "to come to the aid"
PRINT "of their country."
a$ = readstr$(1, 1, 6)          ' Read the first six characters of the
                                ' first line  (that is, "Now is").
b$ = readstr$(4, 10, 18)        ' Read the last nine characters (10-18)
                                ' on line four (that is, "country.").
LOCATE 10, 1
PRINT "A$ = "; a$
PRINT "B$ = "; b$

' readstr$ takes three integers and returns a string.
' The integers are:    - The line on the screen to read from.
'                      - The starting character position.
'                      - The ending character position.
' It returns a string containing the characters on the screen
' on the specified line between the start and stop positions.
'
FUNCTION readstr$ (xline%, xstart%, xstop%)
  DEF SEG = &HB000 ' Changes segment for PEEK to B800h for color
                   ' display.
  fstart% = (160 * (xline% - 1)) + (2 * (xstart% - 1))
  fstop% = (160 * (xline% - 1)) + (2 * (xstop% - 1))
  FOR x% = fstart% TO fstop% STEP 2
    s$ = s$ + CHR$(PEEK(x%))
  NEXT x%
  readstr$ = s$
  DEF SEG    ' Restores DS to default to Basic's DGROUP segment.
END FUNCTION
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB43527