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

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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;
saini_erAuthor Commented:
         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
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_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()
     //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

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.


Unless your requirement is different to what you described, both of my solutions will work.
saini_erAuthor Commented:
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

saini_erAuthor Commented:
I want to use some ANSI codes like \r for return carriage from within the program
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
saini_erAuthor Commented:
             Thanks a lot ..It worked like a charm...Just a small correction FYI
Sendkeys should be like

saini_erAuthor Commented:
You are awesome...great help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.