Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How do you simulate the use of the keyboard ?

by Anonymous Monk
on May 10, 2000 at 12:55 UTC ( #10920=categorized question: print w/replies, xml ) Need Help??
Contributed by Anonymous Monk on May 10, 2000 at 12:55 UTC
Q&A  > input and output


Description:

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 !

Answer: How do you simulate the use of the keyboard ?
contributed by chromatic

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.
Answer: How do you simulate the use of the keyboard ?
contributed by httptech

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.

Answer: How do you simulate the use of the keyboard ?
contributed by merlyn

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.
Answer: How do you simulate the use of the keyboard ?
contributed by btrott

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

Please (register and) log in if you wish to add an answer



  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [Corion]: :-D
    Corion discovers a new shiny toy to try out over the (longish) weekend. Since I've done some more with websockets, maybe I'll try writing a webserver that implements hot-reloading of HTML(+CSS, +Javascript) in the browser. Edit the local file and ...
    [Corion]: ... the browser(s) get a ping to a) refresh the page or b) reload "just" the changed parts, keeping the scroll position etc.
    [Corion]: But I also have to look at how I can make WWW::Mechanize:: RemoteBrowser a reality, and how to make it safe from malicious content ;)
    [Corion]: Part of wanting hot-reloading is that I think I've stumbled on a very simple set of CSS that I maybe want to use for a blog, but I want to try that out on mobile too, and I also want to add/modify it slightly so it has a header too...
    [Corion]: ... and having hot reloading would make it easy to view the changes in multiple browser windows of different sizes, and on Android, simultaneously

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (7)
    As of 2018-04-26 10:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?