Search code examples
windowsinputdconio

D programming language - input without pressing enter


I'm playing around with the D programming language and am wondering how I can grab a character without requiring the user to press enter.

Pseudocode example of what I want:

while(true){
    if(userHasPressedChar()){
        writeln(getChar());
    }
}

In C++ I can use conio.h's "getch()", but i have yet to find anything similar here.

Edit: I am using Windows 7.

Edit 2: I found a solution at this forum, which I could alter for my own use. module main;

import std.stdio;
import core.sys.windows.windows;


void main() {
    auto hCon = GetStdHandle(STD_INPUT_HANDLE);
    FlushConsoleInputBuffer(hCon);
    for(;;) { // in default console mode, ctrl-C will terminate
        INPUT_RECORD inrec;
        DWORD numread;
        while(inrec.EventType != KEY_EVENT) {
            WaitForSingleObject(hCon, INFINITE);
            ReadConsoleInputW(hCon, &inrec, 1, &numread);
        }
        auto keyEvent = inrec.KeyEvent;
        writefln("VK: %x \tChar: %x \tState: %x", 
                 keyEvent.wVirtualKeyCode,
                 keyEvent.UnicodeChar,
                 keyEvent.dwControlKeyState);
    }
}

Solution

  • You can also use various libraries. For example, my terminal.d can do this https://github.com/adamdruppe/arsd/blob/master/terminal.d for windows and linux.

    Here's an example file from my book (see my SO profile if you're interested) that demonstrates the usage http://arsdnet.net/dcode/book/chapter_12/07/input.d

    import terminal;
    
    void main() {
      auto terminal = Terminal(ConsoleOutputType.linear);
      auto input = RealTimeConsoleInput(&terminal, ConsoleInputFlags.raw);
      terminal.writeln("Press any key to exit");
      auto ch = input.getch();
      terminal.writeln("Bye!");
    }
    

    The input object does the necessary conversions to the console mode to turn off line buffering and cleans up after itself. Once you create one, you have methods like input.getch() and input.kbhit() similarly to conio.

    My terminal library also offers other event types for things like mouse input if you want to get into more advanced usages.

    To compile, just download terminal.d and add it to your command, e.g. dmd yourfile.d terminal.d.