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

GetchGetcharExample/D2: example.d

File example.d, 0.9 kB (added by thaumaturgist, 12 years ago)

example source

Line 
1 import std.c.stdio;
2 import std.stdio;
3
4 void main() {
5     char k;
6
7     writefln("\nI'm going to ask you to press 10 keys (I promise it's painless).\n"
8     "Certain keys don't count, but the whole alphabet is fair game.\n");
9
10     for(int i = 0; i < 10; i++) {
11         version(Windows) {
12             writef("Press key: %d\t", i);
13             k = getch();
14         }
15         version(linux) {
16             writef("Press key (follow with return): %d\t", i);
17             k = cast(char) getchar();
18         }
19         version(OSX) {
20             // Unfortunately, merely using getchar() does not disable stdin and readline buffering.
21             writef("Press key (follow with return): %d\t", i);
22             k = cast(char) getchar();
23         }
24         writef("[%s]\t", k);    /* print the character pressed */
25
26         writefln("[%d]\n", k);  /* print ASCII code for key */
27        
28         char enter = cast(char) getchar();
29     }
30     writefln("\nThe End.\n");
31 }