www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Input timeout

reply "Josh" <moonburntm gmail.com> writes:
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".

Thanks

Josh
May 13 2013
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/13/2013 08:50 PM, 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".

 Thanks

 Josh
An unlikely solution is std.concurrency because it already has a timeout facility: import std.stdio; import std.concurrency; import std.datetime; import std.string; void lineReader(Tid owner) { while (true) { string line = readln().chomp(); owner.send(line); } } void main() { spawn(&lineReader, thisTid); while (true) { auto received = receiveTimeout(3.seconds, (string line) { writefln("Thanks for -->%s<--", line); }); if (!received) { writeln("Patiently waiting..."); } } } Ali
May 13 2013
parent reply "Josh" <moonburntm gmail.com> writes:
On Tuesday, 14 May 2013 at 04:14:27 UTC, Ali Çehreli wrote:
 On 05/13/2013 08:50 PM, 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".

 Thanks

 Josh
An unlikely solution is std.concurrency because it already has a timeout facility: import std.stdio; import std.concurrency; import std.datetime; import std.string; void lineReader(Tid owner) { while (true) { string line = readln().chomp(); owner.send(line); } } void main() { spawn(&lineReader, thisTid); while (true) { auto received = receiveTimeout(3.seconds, (string line) { writefln("Thanks for -->%s<--", line); }); if (!received) { writeln("Patiently waiting..."); } } } Ali
Thanks Ali, that's almost what I need. Your answer requires the user to press enter to send the line. Would it be possible to have getch-like behaviour without using C, or would that be the only way?
May 13 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/13/2013 09:22 PM, Josh wrote:

 Your answer requires the user to
 press enter to send the line. Would it be possible to have getch-like
 behaviour without using C, or would that be the only way?
I don't know how to set stdin to non-blocking mode in D. However, if you are sure that stdin is tied to the keyboard (as opposed to e.g. redirected from a file), you can use terminal IO on unix systems: http://forum.dlang.org/thread/im8cnf$ioe$1 digitalmars.com?page=1 Ali
May 13 2013
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
May 14 2013