read single characters from stdin

nazriel spam at dzfl.pl
Fri Sep 28 03:51:55 PDT 2012


On Friday, 28 September 2012 at 09:45:30 UTC, Thomas Koch wrote:
>
> nazriel wrote:
>> http://dpaste.dzfl.pl/eb1387cc
> Thank you. Your solution does not seem to work with multibyte 
> characters, so
> I extended it:
>

Nice, I didn't need multibyte support as I was using it mainly 
for getting keycode

>
> import core.sys.posix.termios;
> import core.stdc.stdio;
>
> char 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 cast(char) ch;
> }
>
> import std.utf;
>
> char readChar()
> {
>     char[4] buffer;
>
>     buffer[0] = getch();
>     auto len = stride(buffer,0);
>
>     foreach (i ; 1 .. len)
>         buffer[i] = getch();
>
>     size_t i;
>     return cast(char) decode(buffer, i);
> }

I think it should return dchar then, and cast(char) should be 
dropped otherwise you will lose visual representation of anything 
that is bigger than 8bits


More information about the Digitalmars-d-learn mailing list