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

saini_erAsked:
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.

TintinCommented:
For a shell script, do

#!/bin/bash
echo | /path/to/program

For Perl, do

#!/usr/bin/perl
open CMD, "|/path/to/program" or die "Can not run program $!\n";
print CMD "\n";
close CMD;
0
saini_erAuthor Commented:
Tintin,
         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
0
MysidiaCommented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

TintinCommented:
saini_er.

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

0
saini_erAuthor Commented:
I want to use some ANSI codes like \r for return carriage from within the program
0
MysidiaCommented:
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
http://sourceforge.net/projects/x11guitest
some doc notes here http://backpan.perl.org/authors/id/C/CT/CTRONDLP/X11-GUITest-0.11.readme

I expect this would work
SendKeys("{ENT}");
0

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:
Mysidia,
             Thanks a lot ..It worked like a charm...Just a small correction FYI
Sendkeys should be like
SendKeys('{ENT}');




0
saini_erAuthor Commented:
You are awesome...great help
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.