简体   繁体   中英

How do I unbuffer input with the D programming language in linux?

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.

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:

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 and tcsetattr calls and then do everything else normally. You'll want to import core.sys.posix.termios; and import 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM