digitalmars.D.learn - How to simulate Window's "Press any key to continue..."
- FireController#1847 (17/17) Nov 21 2019 I'm an extreme beginner to DLang (just started using it.. oh, an
- mipri (13/30) Nov 21 2019 The error doesn't suggest the right replacement, but it still
- FireController#1847 (4/41) Nov 21 2019 Right, but readln will only wait until the user presses the
- mipri (54/57) Nov 21 2019 If curses is available you can use it, at the cost of completely
- mipri (4/5) Nov 21 2019 Oh, if you don't ever call raw() this will break your terminal.
- Mike Parker (10/13) Nov 21 2019 The documentation for std.stdio.File shows two functions for
- Mike Parker (12/25) Nov 21 2019 Sorry, I just noticed the book doesn't cover how to do what you
- =?UTF-8?Q?Ali_=c3=87ehreli?= (12/22) Nov 22 2019 Unfortunately, that won't work either as it requires stdin to be
- Adam D. Ruppe (9/10) Nov 22 2019 I have this exact thing as a sample in my docs:
- IGotD- (9/22) Nov 22 2019 stdin is buffered and will not be forwarded to the D-library
- Jesse Phillips (3/20) Nov 22 2019 execute(["pause"]);
I'm an extreme beginner to DLang (just started using it.. oh, an hour ago?), and I already can't figure out a, what I'd consider, fairly simplistic thing. This is my current code: module DTestApp1; import std.stdio; int main() { write("Press any key to continue..."); stdin.read(); return 0; } I am using Visual Studio to write it, and no matter what I do I cannot get it to work. I attempted to import std.stream;, but it said that while it could find the file, it cannot be read. Am I using the wrong function? For those who don't know, what I'm trying to do is pause the program until literally any key is pressed while in the console.
Nov 21 2019
wrote:I'm an extreme beginner to DLang (just started using it.. oh, an hour ago?), and I already can't figure out a, what I'd consider, fairly simplistic thing. This is my current code: module DTestApp1; import std.stdio; int main() { write("Press any key to continue..."); stdin.read(); return 0; } I am using Visual Studio to write it, and no matter what I do I cannot get it to work. I attempted to import std.stream;, but it said that while it could find the file, it cannot be read. Am I using the wrong function? For those who don't know, what I'm trying to do is pause the program until literally any key is pressed while in the console.The error doesn't suggest the right replacement, but it still tells you that the function you want isn't available: ./test.d(6): Error: no property read for type File, did you mean std.stdio.File.readf(alias format, Data...)(auto ref Data data) if (isSomeString!(typeof(format)))? std.stdio's documentation is here: https://dlang.org/phobos/std_stdio.html and readln is a function you can use for your purpose. stdin.readln(); Or just: readln;
Nov 21 2019
On Friday, 22 November 2019 at 04:19:40 UTC, mipri wrote:On Friday, 22 November 2019 at 04:10:23 UTC,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 keyI'm an extreme beginner to DLang (just started using it.. oh, an hour ago?), and I already can't figure out a, what I'd consider, fairly simplistic thing. This is my current code: module DTestApp1; import std.stdio; int main() { write("Press any key to continue..."); stdin.read(); return 0; } I am using Visual Studio to write it, and no matter what I do I cannot get it to work. I attempted to import std.stream;, but it said that while it could find the file, it cannot be read. Am I using the wrong function? For those who don't know, what I'm trying to do is pause the program until literally any key is pressed while in the console.The error doesn't suggest the right replacement, but it still tells you that the function you want isn't available: ./test.d(6): Error: no property read for type File, did you mean std.stdio.File.readf(alias format, Data...)(auto ref Data data) if (isSomeString!(typeof(format)))? std.stdio's documentation is here: https://dlang.org/phobos/std_stdio.html and readln is a function you can use for your purpose. stdin.readln(); Or just: readln;
Nov 21 2019
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 keyIf 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): /+ 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; }
Nov 21 2019
On Friday, 22 November 2019 at 04:41:30 UTC, mipri wrote:~this() { reset(); }Oh, if you don't ever call raw() this will break your terminal. I just copied some code from a toy program and adapted it, and didn't notice that until I posted.
Nov 21 2019
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 keyThe documentation for std.stdio.File shows two functions for reading input: readln and readf. If readln isn't what you want, then readf probably is: https://dlang.org/phobos/std_stdio.html#.File.readf Also, there's a freely available book online to help get you up to speed: Programming in D. Here's the section on reading from stdin with readf: http://ddili.org/ders/d.en/input.html
Nov 21 2019
On Friday, 22 November 2019 at 04:45:21 UTC, Mike Parker wrote:On Friday, 22 November 2019 at 04:22:07 UTC,Sorry, I just noticed the book doesn't cover how to do what you want and it's probably not obvious. You need to call readf with a character format string (%c): import std.stdio; void main() { writeln("Press any key to continue..."); char c; readf("%c", &c); writeln("Thanks!"); }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 keyThe documentation for std.stdio.File shows two functions for reading input: readln and readf. If readln isn't what you want, then readf probably is: https://dlang.org/phobos/std_stdio.html#.File.readf Also, there's a freely available book online to help get you up to speed: Programming in D. Here's the section on reading from stdin with readf: http://ddili.org/ders/d.en/input.html
Nov 21 2019
On 11/21/19 9:10 PM, Mike Parker wrote:> On Friday, 22 November 2019 at 04:45:21 UTC, Mike Parker wrote:You need to call readf with a character format string (%c): import std.stdio; void main() { writeln("Press any key to continue..."); char c; readf("%c", &c); writeln("Thanks!"); }Unfortunately, that won't work either as it requires stdin to be unbuffered, which is not the case in most terminals. The main issue here is that a D program cannot know that stdin is bound to a keyword. One needs to use a terminal module like the suggested curses or Adam's terminal.d: https://github.com/adamdruppe/arsd/blob/master/terminal.d If it's too much unnecessary complication, then "Press Enter to continue..." is perfectly fine to me. ;) (But of course you can't move the cursor with that. :/) Ali
Nov 22 2019
On Friday, 22 November 2019 at 09:25:37 UTC, Ali Çehreli wrote:https://github.com/adamdruppe/arsd/blob/master/terminal.dI have this exact thing as a sample in my docs: http://dpldocs.info/experimental-docs/arsd.terminal.html#single-key You could wrap that up in a function if you don't want anything else. though personally I'd just do "press enter to continue" since it is so much easier and avoids the dependency. you can use my module by copying the whole file to your source and using it, or it is a dub package arsd-official:terminal too
Nov 22 2019
On Friday, 22 November 2019 at 04:45:21 UTC, Mike Parker wrote:On Friday, 22 November 2019 at 04:22:07 UTC,stdin is buffered and will not be forwarded to the D-library until you press enter. The solution is different on Linux and Windows. On Linux you need to disable the "CANON" mode in the terminal. https://stackoverflow.com/questions/1449324/how-to-simulate-press-any-key-to-continue There are a few suggestions for Windows in the link as well. Annoyingly complicated for such a simple thing but that's how it is.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 keyThe documentation for std.stdio.File shows two functions for reading input: readln and readf. If readln isn't what you want, then readf probably is: https://dlang.org/phobos/std_stdio.html#.File.readf Also, there's a freely available book online to help get you up to speed: Programming in D. Here's the section on reading from stdin with readf: http://ddili.org/ders/d.en/input.html
Nov 22 2019
wrote:I'm an extreme beginner to DLang (just started using it.. oh, an hour ago?), and I already can't figure out a, what I'd consider, fairly simplistic thing. This is my current code: module DTestApp1; import std.stdio; int main() { write("Press any key to continue..."); stdin.read(); return 0; } I am using Visual Studio to write it, and no matter what I do I cannot get it to work. I attempted to import std.stream;, but it said that while it could find the file, it cannot be read. Am I using the wrong function? For those who don't know, what I'm trying to do is pause the program until literally any key is pressed while in the console.execute(["pause"]);
Nov 22 2019