It would probably help if you were at least a little less vague about what you are doing. What the program will be trying to do when a keypress comes in can make a huge difference in what approaches make sense. Also, more details on what type of user interface you would like after you press the key would help.

But my first idea is to have a subroutine that does a non-blocking read (or peek) to see if there is any input (probably via Term::ReadKey) and if there is none, then it immediately returns. If there is some, then it prompts for what you want to do.

Then you sprinkle calls to this subroutine all over your code, making sure that there aren't any long-running sections where the routine will never be called.

Depending on what your code is doing, this sprinkle might be reduced to just a single call.

You could also teach this routine to be smart enough to track how long it was since the last time it was called and notify you if it ever takes more than Xms between calls so that you can know that more sprinkling is required. (and in such cases report the call stack to you so you can figure out what just ran that took so long w/o sprinkles)

                - tye

In reply to Re: GUI-like behavior (vague) by tye
in thread GUI-like behavior by eweaverp

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.