2015-11-05 71 views
0

我知道代碼character = System.Console.ReadKey().tostring的存在。 這將讀取一個字符。 此外,在另一堆棧溢出後,我發現:visual basic .NET獲取用戶輸入而不按下輸入

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 

,當用戶按下讀取輸入輸入 我怎麼可能讓一些代碼,而不讓用戶按下讀取(多個)字符(S)輸入所有的時間?但是用時間作爲停止閱讀控制檯的指標?

+0

你在頂部引用的代碼行將做到這一點。只需重複或類似地調用它。但是,您所遇到的問題是如何告訴系統輸入是「完整的」,現在是系統執行某些操作的時候了。這就是按'輸入'正在做的事情。 – peterG

+0

不,我只是想把輸入的功能替換成時間的功能 –

回答

0

根據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或東西,如果它超時。

1

您可以在這裏使用System.Console.Read()。它從標準輸入流中讀取爲字符。 https://msdn.microsoft.com/en-us/library/system.console.read(v=vs.110).aspx

+0

雖然這不是交互式的,所以一旦輸入流被刷新,即用戶按下輸入,字符只會到達調用者。 –

+0

您是否嘗試過鏈接中的示例 – haseeb

+0

沒錯,顯然文檔是錯誤的。因爲通過簡單地查看標準輸入流中的下一個字符,您可以不**得到此行爲。 –