read single characters from stdin

Thomas Koch thomas at koch.ro
Fri Sep 28 02:46:05 PDT 2012


nazriel wrote:
> http://dpaste.dzfl.pl/eb1387cc
Thank you. Your solution does not seem to work with multibyte characters, so 
I extended it:


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);
}



More information about the Digitalmars-d-learn mailing list