Get single keystroke?

teo teo.ubuntu at yahoo.com
Mon Mar 21 15:32:52 PDT 2011


On Mon, 21 Mar 2011 23:22:17 +0100, Andrej Mitrovic wrote:

> Woops forgot to add import std.stdio, here's a fixed version:
> 
> import std.stdio : writefln;
> import std.c.stdio;
> import std.c.linux.termios;
> 
> extern(C) void cfmakeraw(termios *termios_p);
> 
> void main()
> {
>     termios  ostate;                 /* saved tty state */ termios 
>     nstate;                 /* values for editor mode */
> 
>        // Open stdin in raw mode
>        /* Adjust output channel        */
>     tcgetattr(1, &ostate);                       /* save old state */
>     tcgetattr(1, &nstate);                       /* get base of new
>     state */ cfmakeraw(&nstate);
>     tcsetattr(1, TCSADRAIN, &nstate);      /* set mode */
> 
>       // Read characters in raw mode
>     writefln("The key hit is %s", cast(char)fgetc(stdin));
> 
>        // Close
>     tcsetattr(1, TCSADRAIN, &ostate);       // return to original mode
> }

It looks like this can be dangerous, because the terminal can be left in 
an unusable state. Please read this:
http://groups.google.com/group/comp.os.linux.development.apps/
browse_thread/thread/0667d16089e2b6fc


More information about the Digitalmars-d-learn mailing list