Reading Standard Input

nazriel spam at dzfl.pl
Wed Jan 23 10:03:15 PST 2013


On Wednesday, 23 January 2013 at 17:59:04 UTC, Kenneth Sills 
wrote:
> Hello everyone! I'm pretty new to the D world, and just started
> playing around with it. To start off with the language, I was
> going to write a little game (as I usually do). I wanted to use
> pure D (no ncurses) and not have to import any libraries (no
> anything else) for the project. So I set out to make it a CLI
> game.
>
> I've written myself a nice little library for formatting the
> output and doing all that lovely stuff with the terminal -
> however I'm having trouble with input. You see, I need
> nonblocking (which I've set up) input that can read character by
> character (so when the user presses r, it immediately takes it
> in, not waiting for an enter) and I need to be able to see 
> things
> like arrow keys, shift keys, and control keys.
>
> I've looked around extensively, but I have yet to find anything
> on how to set up character by character input NOR have I found
> anything on receiving those special characters in D. I know it's
> quite possible in C, but again, half the point of this project 
> is
> being pure D.So how would I go about implementing this?
>
> Thank you in advance!

Adam Ruppe is working on very nice terminal handler. Maybe you 
can look into this: 
https://github.com/robik/ConsoleD/blob/master/terminal.d

It's rather complete solution.

Myself I was using this simple function, not sure if it will be 
usable for you:
     char ReadKey()
     {
         version(Posix)
         {
             int getch()
             {
                 int ch;
                 termios oldt;
                 termios newt;

                 tcgetattr(0, &oldt);
                 newt = oldt;
                 newt.c_lflag &= ~(ICANON | ECHO);
                 tcsetattr(0, TCSANOW, &newt);
                 ch = getchar();
                 tcsetattr(0, TCSANOW, &oldt);
                 return ch;
             }
         }
         else version(Windows)
         {
             alias _getch getch;
         }

         return cast(char) getch();
     }


More information about the Digitalmars-d-learn mailing list