How to simulate Window's "Press any key to continue..."
mipri
mipri at minimaltype.com
Fri Nov 22 04:41:30 UTC 2019
On Friday, 22 November 2019 at 04:22:07 UTC, FireController#1847
wrote:
> Right, but readln will only wait until the user presses the
> delimiter (by default Enter/Return). I want it to wait until
> ANY key is pressed, not a specific key
If curses is available you can use it, at the cost of completely
changing how you do I/O (in a good way if you need lots of
updates):
#! /usr/bin/env dub
/+ dub.sdl:
dependency "nice-curses" version="~>0.2.5"
+/
import std.stdio;
import nice.curses: Curses;
void main() {
auto curses = new Curses;
auto scr = curses.stdscr;
curses.setCursor(0);
scr.addstr("Press any key to continue...");
scr.refresh;
curses.update;
scr.getch;
}
If you really just briefly want getch-style input in a normal
terminal program, and still have a posix system, you can do that
with tcsetattr.
https://stackoverflow.com/questions/7469139/what-is-the-equivalent-to-getch-getche-in-linux
struct Terminal {
import core.stdc.stdio: getchar;
import core.sys.posix.termios:
tcgetattr, tcsetattr, termios,
ECHO, ICANON, TCSANOW, TCSAFLUSH;
private termios last;
int getch() { return getchar(); }
int getch_once() {
raw;
auto r = getchar;
reset;
return r;
}
void raw() {
termios term;
tcgetattr(0, &last);
term = last;
term.c_lflag &= ~(ICANON | ECHO);
tcsetattr(0, TCSANOW, &term);
}
void reset() { tcsetattr(0, TCSAFLUSH, &last); }
~this() { reset(); }
}
void main() {
import std.stdio: write, writeln;
Terminal term;
write("Press any key to continue:");
term.getch_once();
writeln;
}
More information about the Digitalmars-d-learn
mailing list