kees broeksma
kees broeksma

Reputation: 66

visual basic .NET get user input without pressing enter

I know of the existence of the code character = System.Console.ReadKey().tostring. This will read one character. Also in another stack overflow post I found:

    Public Shared Function ReadPipedInfo() As StreamReader
        'call with a default value of 5 milliseconds
        Return ReadPipedInfo(5000)
    End Function

    Public Shared Function ReadPipedInfo(waitTimeInMilliseconds As Integer) As StreamReader
        'allocate the class we're going to callback to
        Dim callbackClass As New ReadPipedInfoCallback()
        'to indicate read complete or timeout
        Dim readCompleteEvent As New AutoResetEvent(False)
        'open the StdIn so that we can read against it asynchronously
        Dim stdIn As Stream = Console.OpenStandardInput()
        'allocate a one-byte buffer, we're going to read off the stream one byte at a time
        Dim singleByteBuffer As Byte() = New Byte(0) {}
        'allocate a list of an arbitary size to store the read bytes
        Dim byteStorage As New List(Of Byte)(4096)
        Dim asyncRead As IAsyncResult = Nothing
        Dim readLength As Integer = 0
        'the bytes we have successfully read
        Do

            'perform the read and wait until it finishes, unless it's already finished
            asyncRead = stdIn.BeginRead(singleByteBuffer, 0, singleByteBuffer.Length, New AsyncCallback(AddressOf callbackClass.ReadCallback), readCompleteEvent)
            If Not asyncRead.CompletedSynchronously Then
                readCompleteEvent.WaitOne(waitTimeInMilliseconds)
            End If
            'end the async call, one way or another
            'if our read succeeded we store the byte we read
            If asyncRead.IsCompleted Then
                readLength = stdIn.EndRead(asyncRead)
                'If readLength > 0 Then
                    byteStorage.Add(singleByteBuffer(0))
                'End If
            End If
        Loop While asyncRead.IsCompleted AndAlso readLength > 0
        'we keep reading until we fail or read nothing

        'return results, if we read zero bytes the buffer will return empty
        Return New StreamReader(New MemoryStream(byteStorage.ToArray(), 0, byteStorage.Count))
    End Function

    Private Class ReadPipedInfoCallback
        Public Sub ReadCallback(asyncResult As IAsyncResult)
            'pull the user-defined variable and strobe the event, the read finished successfully
            Dim readCompleteEvent As AutoResetEvent = TryCast(asyncResult.AsyncState, AutoResetEvent)
            readCompleteEvent.[Set]()
        End Sub
    End Class

which reads input if the user pressed enter how could I make some code that reads (multiple) character(s) without letting the user press enter all the time? But instead use time as the indicator to stop reading the console?

Upvotes: 0

Views: 972

Answers (2)

kees broeksma
kees broeksma

Reputation: 66

According to here I found out how to deal with it:

Public Module ConsoleHelper
Sub Main()
    msgbox(ReadKeyWithTimeOut(10000).key.ToString)

End Sub

Public Function ReadKeyWithTimeOut(timeOutMS As Integer) As ConsoleKeyInfo
    Dim timeoutvalue As DateTime = DateTime.Now.AddMilliseconds(timeOutMS)
    While DateTime.Now < timeoutvalue
        If Console.KeyAvailable Then
            Dim cki As ConsoleKeyInfo = Console.ReadKey()
            Return cki
        Else
            System.Threading.Thread.Sleep(100)
        End If
    End While
    Return New ConsoleKeyInfo(" "C, ConsoleKey.Spacebar, False, False, False)
End Function

End Module

Now just finding out how to give attribute key NULL or something if it timed out.

Upvotes: 0

haseeb
haseeb

Reputation: 682

You can use System.Console.Read() here. It reads as character from standard input stream. https://msdn.microsoft.com/en-us/library/system.console.read(v=vs.110).aspx

Upvotes: 1

Related Questions