Eldon Lesley
Eldon Lesley

Reputation: 925

how to have keypress on vb.net console without the program has to wait

I built a console application with a loop at the end,

While(key <> "q")
   'some code
   'code to modify the variable [key]
   Console.Write("A")
End While

in the loop when the user press "q" on the keyboard, the program will go out of the loop, I've tried using Console.Read or Console.ReadKey which is not working because it waits for the user to press the keyboard, but I need the loop to keep running although the user is not pressing the keyboard

so in the program the console will be like: AAAAAAAAAAAAAAAA,etc. until the user press "q" on the keyboard,

any idea?

Upvotes: 0

Views: 6365

Answers (1)

Hans Passant
Hans Passant

Reputation: 941377

You want to use the Console.KeyAvailable property to check if ReadKey will block. Like this:

Sub Main()
    Do
        '' Do something
        ''...
        Console.Write("A")
        If Console.KeyAvailable Then
            If Console.ReadKey(True).KeyChar = "q"c then Exit do
        End If
    Loop
End Sub

Upvotes: 1

Related Questions