Strange behavior readline
-
Hi I've wrote this code
With NewHS
.Lines = nLijnen
.PlayDate = Now
Console.SetCursorPosition(10, 21)
Console.Write("New highscore. " & nLijnen & " Your name please? ")
.Name = Console.ReadLine
.Name = .Name.Trim
If .Name = "" Then .Name = "[No Name]"
End WithWhen running the line console.readline It asks for two enters to accept. I see the cursor movingto the begin of the next line by the first enter. The second time the program runs those lines of code it working well with one enter. I'm clearing the keyboardbuffer before starting writing to the console with
Sub ClearKBBuffer()
While Console.KeyAvailable
Console.ReadKey(True)
Console.ReadKey()
End While
End SubAny Idea why or how to solve this. Jan
-
Hi I've wrote this code
With NewHS
.Lines = nLijnen
.PlayDate = Now
Console.SetCursorPosition(10, 21)
Console.Write("New highscore. " & nLijnen & " Your name please? ")
.Name = Console.ReadLine
.Name = .Name.Trim
If .Name = "" Then .Name = "[No Name]"
End WithWhen running the line console.readline It asks for two enters to accept. I see the cursor movingto the begin of the next line by the first enter. The second time the program runs those lines of code it working well with one enter. I'm clearing the keyboardbuffer before starting writing to the console with
Sub ClearKBBuffer()
While Console.KeyAvailable
Console.ReadKey(True)
Console.ReadKey()
End While
End SubAny Idea why or how to solve this. Jan
-
Hi I've wrote this code
With NewHS
.Lines = nLijnen
.PlayDate = Now
Console.SetCursorPosition(10, 21)
Console.Write("New highscore. " & nLijnen & " Your name please? ")
.Name = Console.ReadLine
.Name = .Name.Trim
If .Name = "" Then .Name = "[No Name]"
End WithWhen running the line console.readline It asks for two enters to accept. I see the cursor movingto the begin of the next line by the first enter. The second time the program runs those lines of code it working well with one enter. I'm clearing the keyboardbuffer before starting writing to the console with
Sub ClearKBBuffer()
While Console.KeyAvailable
Console.ReadKey(True)
Console.ReadKey()
End While
End SubAny Idea why or how to solve this. Jan
Sub ClearKBBuffer()
While Console.KeyAvailable
Console.ReadKey(True)
Console.ReadKey()
End While
End SubSurely that will block when there are an odd numbers of keys in the input queue. Did you mean this
Sub ClearKBBuffer()
While Console.KeyAvailable
Console.ReadKey(True)
End While
End SubAlan.
-
Sub ClearKBBuffer()
While Console.KeyAvailable
Console.ReadKey(True)
Console.ReadKey()
End While
End SubSurely that will block when there are an odd numbers of keys in the input queue. Did you mean this
Sub ClearKBBuffer()
While Console.KeyAvailable
Console.ReadKey(True)
End While
End SubAlan.
-
I just tried your initial code (with various responses) and it works with only one press of the Enter key.
The whole code where start is the real game <TargetFramework>net6.0</TargetFramework>
Sub ClearKBBuffer() While Console.KeyAvailable Console.ReadKey(True) End While End Sub Sub Main() Dim k As ConsoleKey Console.TreatControlCAsInput = True Randomize() Do Console.CursorVisible = False Dim nLijnen = Start() ClearKBBuffer Console.CursorVisible = True Dim hs As HighScores = HighScores.Load() Console.BackgroundColor = ConsoleColor.Red Console.ForegroundColor = ConsoleColor.Black Dim NewPLace As Integer = -1 For i As Integer = 0 To 9 If nLijnen > hs.Scores(i).Lines And NewPLace < 0 Then NewPLace = i End If Console.SetCursorPosition(10, 10 + i) Console.Write(hs.Scores(i).Name.PadRight(30) & hs.Scores(i).Lines.ToString.PadLeft(10) & " " & hs.Scores(i).PlayDate.ToString("dd/MM/yyyy HH:mm:ss")) Next Console.Beep() If NewPLace >= 0 Then Dim NewHS As New Score With NewHS .Lines = nLijnen .PlayDate = Now Console.SetCursorPosition(10, 21) Console.Write("New highscore. " & nLijnen & " Your name please? ") .Name = Console.ReadLine .Name = .Name.Trim If .Name = "" Then .Name = "\[No Name\]" End With For i = 8 To NewPLace Step -1 hs.Scores(i + 1) = hs.Scores(i) Next hs.Scores(NewPLace) = NewHS hs.Save() End If Console.SetCursorPosition(0, Console.WindowHeight - 2) Console.WriteLine("New game? (Y/N) ") Do k = Console.ReadKey.Key Loop While k <> ConsoleKey.Y And k <> ConsoleKey.N Loop While k = ConsoleKey.Y End Sub