http://www.perlmonks.org?node_id=10920

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

In my script, I would like to simulate the fact that someone presses on the "y" key on the keyboard and then presses Return. If anyone knows how to do that or if it is even possible to do it. Cheers !

Originally posted as a Categorized Question.

  • Comment on How do you simulate the use of the keyboard ?

Replies are listed 'Best First'.
Re: How do you simulate the use of the keyboard ?
by chromatic (Archbishop) on May 10, 2000 at 18:42 UTC
    If you prefer not changing your filehandle, you can also cat (or perhaps echo, if you're stuck in Windows land) a file into your script:
    #!/usr/bin/perl -w use strict; print while (<STDIN>);
    Invoke it like this: cat printline.pl | ./printline.pl This trick works really really well for debugging CGI.pm applications from the command line.
Re: How do you simulate the use of the keyboard ?
by httptech (Chaplain) on May 10, 2000 at 15:59 UTC
    Well, the keyboard is usually read using <STDIN> which is just another filehandle.

    So say you have written a script that asks a few questions like:

    print "What is your name? "; my $name = <STDIN>; chomp $name; print "What is your quest? "; my $quest = <STDIN>; chomp $quest; print "What is your favorite color? "; my $color = <STDIN>; chomp $color;
    Now, if you want to test that out without actually having to type it into the keyboard, you can make a textfile like:
    Anonymous Monk I seek the Wisdom of the Monastery Yellow...no, Blue! arghhhhhhhh!
    Now, you can open that file in your program and assign it a filehandle <FOO>, ahead of where the questions are asked. Then, use a line like this: *STDIN = *FOO; (also before where you first start grabbing STDIN)

    That allows you to alias STDIN to FOO and the file will be read instead of the keyboard while you are testing your program. When you're done testing, just remove the open statement and the *STDIN = *FOO line and you're all set.

Re: How do you simulate the use of the keyboard ?
by merlyn (Sage) on May 11, 2000 at 01:05 UTC
    Depends on what you mean by "simulate". One way to mean it is to force a "y\n" into your own tty input buffer, like so:
    sub jam { $TIOCSTI = 0x80017472; for (split(//, $_[0])) { ioctl(STDERR, $TIOCSTI, $_) || die "bad TIOCSTI: $!"; } } jam("y\n");
    Note that the value of TIOCSTI can be had from some "use something" that escapes me at the moment, but it'll end up looking like this pretty close.
      I have the above working for ordinary input (i.e. ascii codes) but how could i simluate the use of arrows keys, for example in vi? These do not have a proper ascii code. Duncs
Re: How do you simulate the use of the keyboard ?
by btrott (Parson) on May 11, 2000 at 01:20 UTC
    From the Perl Cookbook "solution" at www.perl.com:
    require 'sys/ioctl.ph'; die "no TIOCSTI" unless defined &TIOCSTI;
    Of course, it also says that
    Since sys/ioctl.h translation is so dodgy, you'll probably have to run this C program to get your TIOCSTI value. % cat > tio.c <<EOF && cc tio.c && a.out main() { printf("%#08x\n", TIOCSTI); } EOF