Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Getting real-time input from the terminal


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.

Getting ready

Download terminal.d from my Github and put it in your project directory.

How to do it…

Let's execute the following steps to get real-time input from the terminal:

  1. Import terminal.

  2. Create a Terminal object.

  3. Create a RealTimeConsoleInput object, passing it a pointer to your Terminal object and passing flags for the types of input you need.

  4. 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.

How it works…

By default, text input from...