D and windows console kbhit, getch etc

Jason King jhking at airmail.net
Sat Jun 2 19:55:51 PDT 2012


I've seen a few messages asking how to get this to work and the 
replies that were functional involved snagging the console input 
handle and trolling for keyboard events yourself.  Since that 
code is inside the standard library (at least for DMD) I figured 
out the proper mix to make it work.

import std.c.stdio;  // I'm just using this for the printf
extern (C) void _STI_conio(); // initializes DM access ton conin, 
conout
extern (C) void _STD_conio(); // properly closes handles
extern (C) int kbhit();       // the conio function is in the DMD 
library
extern (C) int getch();       // as is his friend getch
void main() {
   int mychar;
    _STI_conio();
    while(!kbhit())
    {}
    mychar = getch();
    printf("%c\n",mychar);
    _STD_conio();
}

Works on DMD 2.059 Windows 7 64 bit.


More information about the Digitalmars-d-learn mailing list