Begining with D

Ali Çehreli acehreli at yahoo.com
Thu Oct 10 12:19:53 PDT 2013


On 10/10/2013 11:56 AM, Alejandro wrote:

 > catched a single character, a single keydown

The following program is based on the following newsgroup post:

 
http://forum.dlang.org/post/mailman.2665.1300747084.4748.digitalmars-d-learn@puremagic.com

The program prints character codes in hex until it sees Ctrl-D:

import std.stdio : writef, writeln;
import std.c.stdio;
import std.c.linux.termios;

/* The declaration of cfmakeraw, which is missing from standard D 
modules. */
extern(C) void cfmakeraw(termios *termios_p);

void main()
{
     /* Saving the existing state of tty. */
     termios oldState;
     tcgetattr(1, &oldState);

     /* Ensuring that it will be restored upon exit. */
     scope (exit) tcsetattr(1, TCSADRAIN, &oldState);

     /* Make a new state and set it to raw mode. */
     termios  newState;
     tcgetattr(1, &newState);
     cfmakeraw(&newState);

     /* Use the new state in this terminal. */
     tcsetattr(1, TCSADRAIN, &newState);

     /*
      * We are ready to read characters in this raw mode...
      */

     /* This is Ctrl-D, the EOF character under Linux. */
     enum endOfFile = '\4';

     for (char c; c != endOfFile; ) {
         c = cast(char)fgetc(stdin);
         writef("%02x ", c);
     }

     writeln();
}

 > my bad English

Please don't say that. :) Thank you very much for communicating in English.

Ali



More information about the Digitalmars-d-learn mailing list