Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Double clicking a raw mode Win32::Console app

by perlophone (Initiate)
on Sep 25, 2008 at 22:22 UTC ( #713762=perlquestion: print w/replies, xml ) Need Help??

perlophone has asked for the wisdom of the Perl Monks concerning the following question:

Hi - I have a test app that reads input in raw mode, it is supposed to wait for a single character of user input before proceeding. Works fine in a CMD window under XP, but when I double click it, the wait for the character always returns immediately, hence the app finishes without waiting for the user's input.
I have a feeling this is not a PERL issue but a Windoze issue, i.e. the STD_INPUT_HANDLE does not in fact point to the real console but has been redirected due to the double click.
Any guidelines in that regard? I'm using a recent ActiveState build.Thanks!
#! perl -w use strict; use Win32::Console; my ($Console); $Console = new Win32::Console(STD_INPUT_HANDLE); $Console->Alloc(); $Console->Display(); $Console->Mode(0); $| = 1; print "Hit any character to stop program>"; $Console->Input(); print "\nStopped program\n"; $Console->Flush(); exit(0);
(Updated) Thanks all for your help. For those that are curious, the following ReadKey-based snippet works:
#! perl -w use Term::ReadKey; use strict; my ($Key); $| = 1; print "Hit any character to stop program>"; ReadMode 3; for(;;) { $Key = ReadKey 0.1; if(defined($Key)) { last; } } ReadMode 0; print "Stopped program\n"; exit(0);

Replies are listed 'Best First'.
Re: Double clicking a raw mode Win32::Console app
by BrowserUk (Pope) on Sep 26, 2008 at 01:01 UTC

    You've some fairly serious misunderstanding of Win32::Console. If you check the return codes from your calls to both ->Alloc() & ->Display() you'll find neither are doing anything at all. And if you read their documentation, you'll understand why.

    If you simply want to accept any key without waiting for an enter, you'd be better off using Term::ReadKey which knows how to do that.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Double clicking a raw mode Win32::Console app
by ikegami (Pope) on Sep 26, 2008 at 00:47 UTC
    You could ignore the events you don't want.
    for (;;) { my @event = $Console->Input(); last unless ... }

    Or you could use InputChar instead.

Re: Double clicking a raw mode Win32::Console app
by gok8000 (Scribe) on Sep 26, 2008 at 19:15 UTC
    If you don't need Win32::Console, this one also waits for the pressing of a key:
    print "\nFile table.html created (press a key)\n"; $line = <STDIN>;

      Almost, but not quite. It will wait for a record (delimiter set by $/). Even then, you may have some buffering issues on the console, where the console's record separator is the newline (for that platform).

      See the Term::ReadKey option above.

      Update: Made first paragraph less harsh.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://713762]
Approved by toolic
Front-paged by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2020-01-20 06:31 GMT
Find Nodes?
    Voting Booth?