Jump to page: 1 2
Thread overview
How to simulate Window's "Press any key to continue..."
Nov 22, 2019
mipri
Nov 22, 2019
mipri
Nov 22, 2019
mipri
Nov 22, 2019
Mike Parker
Nov 22, 2019
Mike Parker
Nov 22, 2019
Ali Çehreli
Nov 22, 2019
Adam D. Ruppe
Nov 22, 2019
IGotD-
Nov 22, 2019
Jesse Phillips
November 22, 2019
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.
November 22, 2019
On Friday, 22 November 2019 at 04:10:23 UTC, FireController#1847 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;


November 22, 2019
On Friday, 22 November 2019 at 04:19:40 UTC, mipri wrote:
> On Friday, 22 November 2019 at 04:10:23 UTC, FireController#1847 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;

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
November 22, 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;
}

November 22, 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.



November 22, 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

The 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
November 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, 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
>
> The 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

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!");
}
November 22, 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

November 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, 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
>
> The 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

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.


November 22, 2019
On Friday, 22 November 2019 at 09:25:37 UTC, Ali Çehreli wrote:
>   https://github.com/adamdruppe/arsd/blob/master/terminal.d

I 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
« First   ‹ Prev
1 2