Note: This website is archived. For up-to-date information about D projects and development, please visit


Part of TutorialFundamentals


Get a character from the keyboard.


import std.c.stdio;
import std.stdio;

void main() {
    char k;

    writefln("\nI'm going to ask you to press 10 keys (I promise it's painless).\n"
    "Certain keys don't count, but the whole alphabet is fair game.\n");

    for(int i = 0; i < 10; i++) {
        version(Windows) {
            writef("Press key: %d\t", i);
            k = getch();
        version(linux) {
            writef("Press key (follow with return): %d\t", i);
            k = getchar();
        version(OSX) {
            // Unfortunately, merely using getchar() does not disable stdin and readline buffering.
            writef("Press key (follow with return): %d\t", i);
            k = getchar();
        writef("[%s]\t", k);    /* print the character pressed */

        writefln("[%d]\n", k);  /* print ASCII code for key */
    writefln("\nThe End.\n");


Based on getch.html by jcc7.

Changed 2004-06-01: Added an untested Linux version based on a suggestion (digitalmars.D/2383).

Changed 2009-03-01: Added a tested OS X version, based on dmd now available for OS X 10.5 (both D 1.0 and D 2.0 alpha).