visual basic .NET 无需按回车即可获取用户输入

visual basic .NET get user input without pressing enter

我知道代码 character = System.Console.ReadKey().tostring 的存在。 这将读取一个字符。 同样在另一个堆栈溢出 post 中,我发现:

    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

如果用户按下回车,它会读取输入 我如何制作一些代码来读取(多个)字符而不让用户一直按回车键?而是使用时间作为停止阅读控制台的指标?

您可以在此处使用 System.Console.Read()。它从标准输入流中读取为字符。 https://msdn.microsoft.com/en-us/library/system.console.read(v=vs.110).aspx

根据here我找到了处理方法:

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

模块结束

现在只是找出如何在超时时给属性键 NULL 或其他东西。