• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3099
  • Last Modified:

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

0
saini_er
Asked:
saini_er
  • 5
  • 2
  • 2
1 Solution
 
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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now