By default you can't get the terminal input in Unix without waiting for the User to press enter. How can I get the input instantly? I am using gdc on debian Linux so I can't use ncurses. Thanks.
Asked
Active
Viewed 162 times
3
-
"I am using gdc on debian Linux so I can't use ncurses" What makes you think that, exactly? – Ben Voigt Mar 02 '14 at 18:39
-
http://stackoverflow.com/questions/1630597/how-to-use-a-c-library-from-d – Ben Voigt Mar 02 '14 at 22:52
-
I was wondering the same question... Ncurses are now available on Windows too. – DejanLekic Mar 03 '14 at 12:51
1 Answers
3
ncurses is a good solution that should work on almost any linux installation with any compiler...
But if you don't want to use ncurses, there's a few other options:
- My terminal.d offers it and works on most terminals, but not as many as ncurses (I'd say I cover 98% of typical setups but there's a LOT of variations out there and i didn't try to be as comprehensive as ncurses): https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/terminal.d
Look near the bottom of the file for a version(Demo) void main()
. RealTimeConsoleInput
gives you an event loop with instant input and other info if you want it (mouse, resize, etc.).
- You can also just change the terminal mode with the proper
tcgetattr
andtcsetattr
calls and then do everything else normally. You'll want toimport core.sys.posix.termios;
andimport core.sys.posix.unistd;
for the functions, then the rest is done the same as in C.
Here's how to do that:
termios old;
tcgetattr(1, &old);
scope(exit) tcsetattr(1, TCSANOW, &old); // put the terminal back to how it was
auto n = old;
n.c_lflag &= ~ICANON; // turn off canonical mode
tcsetattr(1, TCSANOW, &n); // do the change
Then you can use the input instantly.

Adam D. Ruppe
- 25,382
- 4
- 41
- 60