How to execute Return key(ENTER Key) from Perl program using Return key code?

Posted on 2008-11-08
Last Modified: 2013-12-26
I want to execute 'Enter key' from the perl program using 'ENTER' key code or escape sequence . I am running interactive program where I need to press 'Enter' two times but I don't want user to press 'Enter' for me but want my perl program to take care of it.

I am using bash shell with linux

Question by:saini_er
    LVL 48

    Expert Comment

    For a shell script, do

    echo | /path/to/program

    For Perl, do

    open CMD, "|/path/to/program" or die "Can not run program $!\n";
    print CMD "\n";
    close CMD;

    Author Comment

             I am aware of various procedure for executing external commands from perl program but what I a looking over here is specific code which can press 'ENTER' key with some Return key ASCII code from perl program itself, without need of pressing 'ENTER' key physically
    LVL 23

    Expert Comment

    Tintin's sample opens the program with a pipe redirected to the program's STDIN and sends a line feed.  "\n"  denotes enter.

    This will work with a shell-based program that accepts user input using stdin.

    If the shell-based program opens /dev/tty manually,   then there is not much you can do.

    If the program you want to send Enter to is graphical and runs under Xwindows, then an option is to install the libXtst library / libXtst-devel package  and write a C program your perl application can call.

    I have attached a sample.

    /* cc -o push_enter push_enter.c -lX11 -lXtst */
    #define XK_MISCELLANY
    #define XK_LATIN1
    #define XK_XKB_KEYS
    /* */
    #include <stdio.h>
    #include <stdlib.h>
    #include <X11/extensions/XTest.h>
    #include <X11/keysymdef.h>
    const char XAUTHORITY_FILE[] = "/home/myusername/.Xauthority";
    int main()
         setenv("XAUTHORITY", XAUTHORITY_FILE, 1);
         //XSetAuthorization(NULL, 0, NULL, 0);
         /* Open local display #1, first workspace */
         Display* dpy = XOpenDisplay(":1");
         int Enter_Key = XKeysymToKeycode( dpy, XK_Return );
         unsigned long delay = CurrentTime;
         if (!dpy) { return 1; }
         printf("%d\n", XTestFakeKeyEvent(  dpy,  Enter_Key, True,
                                            delay ));
         delay += 100;
         printf("%d\n", XTestFakeKeyEvent(  dpy,  Enter_Key, False /* is_press */,
                                            delay ));
         /* Clean up the X session */
         XCloseDisplay( dpy );

    Open in new window

    LVL 48

    Expert Comment


    Unless your requirement is different to what you described, both of my solutions will work.

    Author Comment

    My basic program is to get user input and I am logging all user key presses in input using xev (x event viewer) which is further logged to file...Problem over here is that xev is not flushing buffer properly.So at the end of program I want to press two return key to flush the buffer and don't want user to press them physically


    Author Comment

    I want to use some ANSI codes like \r for return carriage from within the program
    LVL 23

    Accepted Solution

    You are simply not going to be able to send raw keystrokes to X without using a third party library or tool of some sort.

    Simulating keystrokes is not a native capability of Perl.

    Perhaps,  try the X11:GUITest  perl library from CPAN.

    Or download at
    some doc notes here

    I expect this would work

    Author Comment

                 Thanks a lot ..It worked like a charm...Just a small correction FYI
    Sendkeys should be like


    Author Closing Comment

    You are awesome...great help

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now