How can I pipe xevent viewer (xev) output to file completely?

I am using xev to note down all pressed key events but unfortunately after pipeline output to file , I always found last few presses keys missing...

I am using like

xev -id 0x3200020 >testout

I typed sentence --> full sentence
here in xev output last two alphabet 'c' and 'e' are missing from pipeline
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.

Duncan RoeSoftware DeveloperCommented:
You mean send the output to a file?

xev > file

this seems to be a consequence of stdout buffering.
As I believe, the only solution is to wait for the timeout or to stop xev gracefully.
Duncan RoeSoftware DeveloperCommented:
Oh I see the problem. When xev outputs to a file, it buffers file system blocks. For some reason, the last block is not flushed on SIGTERM or SIGINT.
You have to fool xev into being line-buffered. One way to do that is to use the "screen" command. Basically, if you type "screen -L" then you get a windo the contents of which are logged to a file called screenlog.0. As far as I can tell. this file is either updated on a line by line basis or updated every few characters. Anyway, logging only stops when you exit the shell that screen started for you, so you certainly catch all the xev output. "man screen" for more information.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

saini_erAuthor Commented:
I am using xev as integration im my code..I am already opening child process for that .I can't open new window with screen

so, in your child process, do you see a chance to terminate xev gracefully in certain intervals (and then start it anew) to give its stdout the chance to get fflushed?


Duncan RoeSoftware DeveloperCommented:
I think the problem with xev is that is has no means of graceful termination. Even a click on the close button results in a series of reported events rather than an actual close. It doesn't appear to have any signal handlers, so any termination by signal will lose buffered output. The manual page says "it is essentially a debugging and development tool, and  should  not be needed in normal usage".
Seeing you are running it from a C program, there is actually a mechanism you could use to get xev to run line-buffered. You could open a pty and cause xev to run in that. Personally I haven't programmed a pty myself, but you could check the source of, debug or strace a utility that uses one such as expect. Xterm also uses a pty, but debugging that might be harder because of all he X traffic - also much harder to build a debuggable executable than to build expect.
saini_erAuthor Commented:
Basically , I am using perl program to execute xev for particular terminal window and then I am using `killall -9 xev` to terminate program..My perl program is interactive program which requires user input and  I am integrating xev to  capture all user activity on keyboard.....So I am looking for some good procedure which can flush the buffer of xev before it terminated the program  
saini_erAuthor Commented:
Hi folks
I would like to thank everyone for thier suggestion.Finally I came up with couple  of ideas to resolve this problem for which I need your input

OPTION1-> Is there any way that I can execute 'Returnk key' escape sequence from perl program. If We can execute retun key from perl program , then I can have changes in my code to ignore last return key.

OPTION2-> Is there any switch cases available in Perl like c where I can ask user to press return key to exit program

The problem is that you terminate it with 'kill -9' which leaves no chance to xev to flush its buffer!

saini_erAuthor Commented:
No I have tested this already..basically If I press two return key in the end of program with sleep(3) , it clears the buffer and I am getting my all keys.

I just want some procedure using escape sequences that my program with press return key two times
saini_erAuthor Commented:
I am using killall xev and not the forceful -9 option.sorry for confusion
Duncan RoeSoftware DeveloperCommented:
It doesn't matter which signal you use - unless xev catches that signal you will lose buffered output.
If you can build xev from source, you could insert the line:

setvbuf(stdout, NULL, _IONBF,0);

as the first line of main().
This inhibits all file system buffering.
saini_erAuthor Commented:
I got it working using sendkey.Thanks everyone for help

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

From novice to tech pro — start learning today.