UPDATE: Never mind. I figured it out. It was doing exactly what it was support to do.

The real problem was my code in the previous section.


In my application, I ask for the user's input, but if the user presses a "Ctrl-H", I want to display a help screen, and if the user presses a "Ctrl-P", I want to go back to the previous question.

I've found the "Term::ReadKey" module and did a small little test:

#! /usr/bin/env perl use strict; use warnings; use Term::ReadKey; ReadMode "raw"; my $key = ReadKey(0); ReadMode "restore"; print "Backspace detected!\n" if ($key eq "^H"); my $foo = <STDIN>; print "ASCII Code Value = " . ord($key) . "\n"; print "Complete Answer = " . $key . $foo . "\n"; print "You typed a \"$key\"\n";
Works like a charm! Now, I apply this same technique to my program:
print "\n\nANSWER"; if (defined($default)) { print " [Default = $default]"; } print ": "; ReadMode "raw"; my $key = ReadKey(0); ReadMode "restore"; if ($key eq "^H") { #Help Mode! $showHelpFlag = 1; redo; } chomp($answer = $key . <STDIN>);
No good! When I print the value of "$key", it shows the correct ASCII value, but it doesn't seem to read control characters, so it simply ignores "Ctrl-H" presses.

So, why does the test program work, but the same code in my more complex program doesn't? BTW, the code is in a part where I redefined the package name. I'm making a feeble attempting to do object oriented programming. Would this do anything?


In reply to Reading Single Character Input with Term::ReadKey by qazwart

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.