Thread: Sending simulated keyboard input into a running system() application

  1. #1
    Registered User
    Join Date
    Nov 2014
    Location
    London
    Posts
    2

    Sending simulated keyboard input into a running system() application

    Hey,

    C noob here. I'm trying to simulate a ctrl+ keypress into a running application.

    Quick example:

    Code:
    #include <stdio.h>
    
    int main (void) {
      system("/usr/local/bin/rundb");
    }
    My problem is that the rundb program needs the user to type ctrl-b then s to actually start executing. What is the best way to handle this automatically? Some sort of fork/pipe?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to do this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2014
    Location
    London
    Posts
    2
    It's part of a larger menu interface that I'm writing. The above code isn't the entire source, just an example of what I'm trying to do (in this case the the rundb system() call is executed when a user hits 'c' from my menu). I'd like to make all the keyboard input as simple as possible.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    There is no built-in way to do this in C (ie its not in the standard). However on POSIX systems you can use popen() or manually fork() and execv() then pipe input/output to/from the sub process (would be one way).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending keyboard input
    By el3ktr1k in forum C Programming
    Replies: 4
    Last Post: 06-05-2009, 08:31 AM
  2. Sending Keyboard Int
    By cfrost in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2004, 10:38 PM
  3. Keyboard input in Windows NT/2K/XP native application
    By kcahcn in forum Windows Programming
    Replies: 3
    Last Post: 11-18-2002, 02:24 AM
  4. sending keyboard msgs to childwindows
    By btq in forum Windows Programming
    Replies: 6
    Last Post: 10-21-2002, 02:47 AM