Input timeout

Adam D. Ruppe destructionator at gmail.com
Tue May 14 05:33:55 PDT 2013


On Tuesday, 14 May 2013 at 03:50:22 UTC, Josh wrote:
> Is there a way in D to only accept input for a certain time, 
> instead of std.stdio.readln's behaviour? Something like "Press 
> a key in 3 seconds to abort".

see also this thread:
http://forum.dlang.org/thread/vavskrvzebozkreubnbh@forum.dlang.org

the terminal.d file can do it like this:
https://github.com/robik/ConsoleD/blob/master/terminal.d


import terminal;

void main() {
         auto terminal = Terminal(ConsoleOutputType.linear);
         // ConsoleInputFlags.raw means get one character at a 
time instead of   one line at a time
         auto input = RealTimeConsoleInput(&terminal, 
ConsoleInputFlags.raw);

         // this is in milliseconds, returns true if they actually 
pressed the   key
         if(input.timedCheckForInput(3_000)) {
                 // input was available
                 terminal.writeln("Aborted!");
                 auto ch = input.getch();
                 terminal.writeln("btw you said ", ch);
         } else {
                // timer expired
                 terminal.writeln("proceeding with operation");
         }
}



It should work on windows and linux though I haven't personally 
tested this example on windows yet.


More information about the Digitalmars-d-learn mailing list