Microsoft Visual Basic:如何在控制台中按下一个键后让程序写一行
Microsoft Visual Basic: How to make the program write a line after I press a key in console
我正在制作一个控制台应用程序,但在我按任意键后尝试使程序具有特定功能时遇到问题。
目前是这样:
Console.ForegroundColor = ConsoleColor.White
Console.WriteLine("Press a key to continue...")
我想知道如何做到这一点:按下回车键后,程序会写入一行新文本,内容为 "Welcome to my game"
谢谢
Dim cki As ConsoleKeyInfo
cki = Console.ReadKey()
'At this point cki.key holds the information about the pressed key
'so you could go on like
If cki.Key = ConsoleKey.Enter Then
Console.WriteLine("Welcome to my game")
End If
应该可以解决问题
使用Console.ReadLine()
:
Console.ForegroundColor = ConsoleColor.White
Console.Write("Press Enter to continue...")
Console.ReadLine()
Console.WriteLine("Welcome to my game!")
' ... more code ...
假设你的意思是 "any key" 而不是 "enter":
Console.ForegroundColor = ConsoleColor.White
Console.WriteLine("Press a key to continue...")
Console.ReadKey(True) ' True stops it from showing the pressed key
Console.WriteLine("Welcome to my game!")
您可能还想更改 Console.BackgroundColor
以确保有一些对比,如果它以前可以设置为类似 ConsoleColor.Yellow 的话。
我正在制作一个控制台应用程序,但在我按任意键后尝试使程序具有特定功能时遇到问题。
目前是这样:
Console.ForegroundColor = ConsoleColor.White
Console.WriteLine("Press a key to continue...")
我想知道如何做到这一点:按下回车键后,程序会写入一行新文本,内容为 "Welcome to my game"
谢谢
Dim cki As ConsoleKeyInfo
cki = Console.ReadKey()
'At this point cki.key holds the information about the pressed key
'so you could go on like
If cki.Key = ConsoleKey.Enter Then
Console.WriteLine("Welcome to my game")
End If
应该可以解决问题
使用Console.ReadLine()
:
Console.ForegroundColor = ConsoleColor.White
Console.Write("Press Enter to continue...")
Console.ReadLine()
Console.WriteLine("Welcome to my game!")
' ... more code ...
假设你的意思是 "any key" 而不是 "enter":
Console.ForegroundColor = ConsoleColor.White
Console.WriteLine("Press a key to continue...")
Console.ReadKey(True) ' True stops it from showing the pressed key
Console.WriteLine("Welcome to my game!")
您可能还想更改 Console.BackgroundColor
以确保有一些对比,如果它以前可以设置为类似 ConsoleColor.Yellow 的话。