Console: Read

Read

Reads a character from the current input source.



 Public Function Read ( ) As Long

Return Values

Long -  The next character read from the input source, or -1 if no more characters exists.

Remarks

The Read method does not return until the Return key is pressed. Once the function returns, it can be called repeatedly until all key presses upto the return key are retrieved. The return key is also returned as a carriage-return (13) followed by a line-feed (10).

Examples

This example shows how to read input from the console keyboard. The Read function will block and wait for the return key to be pressed before returning. If there are characters already in the input stream, the function does not block and returns the next character.

Private Sub Main()
    Dim ch As Long
    
    ' reads from the console keyboard. The function
    ' will block until the return key is pressed.
    ch = Console.Read
    
    ' we will loop through the characters typed into
    ' the console and exit the loop when CTRL+Z followed
    ' by Return is pressed.
    Do While ch &lt&gt 26
        Debug.Print Chr$(ch);
        DoEvents
        
        ' As long as there are characters remaining to be
        ' from the console keyboard, this function won't block.
        ch = Console.Read
    Loop
End Sub
As shown here, the loop just retrieves characters from the console keyboard one character at a time. If there are no more characters in the buffer, then the Read function will block and wait for the Return key to be pressed again. The key combination of CTRL+Z (26) is used as an exit from the loop. CRTL+Z is what is used to signal the end of a file on unix platforms.

See Also

Project CorLib Overview

Class Console Overview