Getting real-time input from the console or terminal can take a fair amount of platform-specific code, quite a bit more than writing out colored output.
Let's execute the following steps to get real-time input from the terminal:
Import
terminal
.Create a
Terminal
object.Create a
RealTimeConsoleInput
object, passing it a pointer to yourTerminal
object and passing flags for the types of input you need.Use the
input
object's methods.
The code is as follows:
import terminal; void main() { auto terminal = Terminal(ConsoleOutputType.linear); auto input = RealTimeConsoleInput(&terminal, ConsoleInputFlags.raw); terminal.writeln("Press any key to exit"); auto ch = input.getch(); terminal.writeln("Bye!"); }
Now, run the program. It will immediately exit as soon as you press any key without waiting for a full line.