Find the answer to your Linux question:
Results 1 to 4 of 4
so i am taking a class on scripting and i am trying to figure out how to send an char to something and make it act like a key press ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    37

    bash psudo keyboard


    so i am taking a class on scripting and i am trying to figure out how to send an char to something and make it act like a key press in other words a command that acts like a key was pressed
    my first thought was to echo to stdin but that does not seem to work but i can't seem to find how to do it. does anybody know how to do this, or do i make no sense

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,175
    There are a number of programs that do this. How is not as simple as you might think, as you have obviously found. Since this is a homework assignment, we cannot help you much here. However, I will point you to look at the source code for VNC since it does what you want when you remote-control a desktop or x-server session. Also, remember that Google is your friend! Try a Google search on the following terms: linux inject keystroke
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux Newbie tetsujin's Avatar
    Join Date
    Oct 2008
    Posts
    117
    Depending on what program you're trying to feed input into, the solution can be somewhat different... I'm guessing you just want to feed input to a program in a terminal window, and not to a GUI...

    If you truly need to simulate keyboard input to a terminal, the source code to "expect" might be a better starting point than something like VNC. Maybe also try "man tty_ioctl".

    Basically, when a program runs within a shell, it'll have stdin (file descriptor 0) and its "controlling TTY" (/dev/tty, which is an alias for the current process's TTY device) - these may or may not be the same. For instance, if you redirect the input to a program, that will change stdin but not the controlling TTY. (Some programs will read from or write to /dev/tty independently of where their input or output is routed)

    The implication here is that for many shell programs, you can simulate input simply by redirecting some data to their stdin - but some programs bypass stdin and get some input directly from /dev/tty. Another little wrinkle is that certain program behaviors can change depending on whether stdin is a TTY or not. One of the most common differences is that the standard C library will buffer input and output differently depending on whether stdin/stdout are TTY devices or some other kind of file descriptor. Other programs may check to see if stdin is a TTY in order to switch whether they should behave in an "interactive" mode or not - for instance, run "cat | bash" - bash will detect that its stdin is a pipe, not a TTY, and so by default it will assume it's a non-interactive session, not print prompts, etc.

    This means that to truly simulate terminal keyboard input, you need to deal with the TTY device itself - you can either exec the process you want to run yourself, and set it up so that its I/O directs to a PTY slave device, then feed data into the PTY master, or you can take a process that's already running, get its TTY device, and then use the TTY ioctl to simulate some input to the terminal...

    If you have an interest in this stuff that extends beyond your class, I highly recommend "Advanced Programming in the Unix Environment" by W. Richard Stevens. There's a lot of great stuff in that book - I've found it tremendously helpful in my efforts to understand the somewhat convoluted TTY system...

  4. #4
    Just Joined!
    Join Date
    Mar 2009
    Posts
    37
    thanks
    that helped alot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •