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

Should be a simple question. I'm writing a script using Expect.pm to automate a curses-based process. I need to send IBM scan codes (pgdn, pgup, and arrow keys in particular) to the spanwed process.
#!/usr/local/bin/perl -w use Expect; use strict; # Debugging stuff. $Expect::Log_Stdout=1; $Expect::Debug=1; my $exp = Expect->spawn('curses-prog') or die "Cannot spawn curses-prog: $!\n"; if ($exp->expect(10, 'Menu item 1')) { print $exp "\0402" };
I'm pretty sure that my notation for sending the needed control characters is incorrect. If anyone could point me in the right direction, it'd be much appreciated.

Replies are listed 'Best First'.
Re: IBM scan codes (pgup, pgdn, arrow keys)
by Popcorn Dave (Abbot) on Jun 14, 2002 at 22:23 UTC
    This is going back many, many years, but as I remember from my old Turbo Pascal days...

    When a key is pressed, you're going to need to check for an escape key, do an immediate readkey and process that key.

    If one of those special keys is pressed it sends the escape key followed by the key you pressed. This is only true for ascii codes above 127 I believe.

    Hope that helps! ( And if I'm wrong, someone will correct me...)

    Some people fall from grace. I prefer a running start...

Re: IBM scan codes (pgup, pgdn, arrow keys)
by jlongino (Parson) on Jun 15, 2002 at 04:13 UTC
    I've never used Expect.pm before and it's not clear from your description, but I think this may be more complex than it appears.

    I suppose that from the snippet you've posted that it is invoked on a 'nix system. If so, I guess that you're running it via a PC terminal session of some sort (telnet or Putty/other SSH type connection). If so, the program will expect keyboard codes that are agreed upon by both the 'nix system and the PC emulation software. The 'nix terminal type is probably the same as that defined for the account you're running it from. You'll need to lookup and use the escape sequences for that emulation and send them to Expect (hint: if you specify simple emulations, your control sequences can be simplified as well). You can try the methods explained by the other monks here, use the suggestions in Expect.pod:

    Q: How do I send control characters to a process? A: You can send any characters to a process with the print command. To represent a control character in Perl, use \c followed by the lette +r. For example, control-G can be represented with "\cG" . Note that this will not work if you single-quote your string. So, to send control-C to a process in $exp, do: print $exp "\cC"; Or, if you prefer: $exp->send("\cC");
    or build strings using the chr(dec) function:
    $vt220_pgup = chr(27) . chr(91) . chr(51) . chr(126); print $exp $vt220_pgup
    I could be totally off base on this, so please let me know if this is the case. I've made a lot of suppositions here but perhaps, if nothing else, it may give you a place to start.

    --Jim

Re: IBM scan codes (pgup, pgdn, arrow keys)
by jepri (Parson) on Jun 14, 2002 at 22:51 UTC
    Try a slash in front of each pair of hex digits. e.g. \r\n = \10\12. I think that's what you want. Oh, and you'll be needing a comma in here somewhere  $exp "\0402"

    And you should probalby use $exp->send or $exp->send_slow to send chars to your trapped program. Those printed characters will be displayed to you, not sent to the program, I think (haven't checked).

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.