www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D and windows console kbhit, getch etc

reply "Jason King" <jhking airmail.net> writes:
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.
Jun 02 2012
parent "Danny Arends" <Danny.Arends gmail.com> writes:
Wow been looking for this for some time,
just tested it and it works on windowsXP 32 bit as well

On Sunday, 3 June 2012 at 02:55:53 UTC, Jason King wrote:
 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.
Jun 04 2012