简体   繁体   English

Visual Basic .NET无需按回车键即可获取用户输入

[英]visual basic .NET get user input without pressing enter

I know of the existence of the code character = System.Console.ReadKey().tostring . 我知道代码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? 如果用户按下Enter,哪个会读取输入内容我该如何编写一些读取(多个)字符而不让用户一直按下Enter的代码? But instead use time as the indicator to stop reading the console? 但是,以时间为指标来停止阅读控制台吗?

You can use System.Console.Read() here. 您可以在此处使用System.Console.Read()。 It reads as character from standard input stream. 它从标准输入流中读取为字符。 https://msdn.microsoft.com/en-us/library/system.console.read(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.console.read(v=vs.110).aspx

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. 现在,只需找出如何赋予属性键NULL或超时的值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM