Here is a very simple version of the program I'm working on. Is there a way to write is_any_key_pressed() that doesn't block, doesn't require the Enter key, and doesn't require dragging in any complex libraries or dealing with low-level stuff like ioctl()? Is there nothing in Phobos that provides the needed functionality?
I won't go into the details, but I tried threading, putting the getchar() in a separate thread that sets a global bool g_keypressed variable. It was a disaster. Maybe there's a right way to use threading? I am not talented at threads.
import core.thread;
import core.time;
import std.stdio;
import std.conv;
void light_on() {
write("on "); stdout.flush;
}
void light_off() {
write("off "); stdout.flush;
}
void wait() {
Duration dur = dur!("seconds")( to!int(1) );
Thread.sleep(dur);
}
bool is_any_key_pressed() {
getchar(); // nope, requires Enter key to be pressed, and is blocking
return true;
}
void main(string[] args)
{
while (true) {
light_on();
wait();
light_off();
wait();
if (is_any_key_pressed()) {
break;
}
}
}