LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-09-2004, 05:29 AM   #1
Majestros
LQ Newbie
 
Registered: Jul 2004
Posts: 3

Rep: Reputation: 0
Key press


Hi,
i need write a C/C++ program for create a batch application that for each characters read from serial port create a key press event as the character was generate from user action (key press).

How it's possible? I must emulate the key press.

Tank's to all!
 
Old 07-09-2004, 08:28 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I don't know if you want to simulate key stroke in X, here is an example code that will open gvim and 'type' hello world. (replace gvim by your prefered text editor if you want, in this case remove the XK_a wich is needed to enter insersion mode in gvim)
Code:
/* it is compatible with latin keyboard */
#include <X11/extensions/XTest.h>
#define XK_LATIN1
#define XK_MISCELLANY 
#define XK_XKB_KEYS
#include <X11/keysymdef.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    Display* pDisplay = XOpenDisplay( NULL );

    KeySym key[] = { XK_a,XK_h,XK_e,XK_l,XK_l,
                     XK_o,XK_space,XK_w,XK_o,
                     XK_r,XK_l,XK_d,XK_Return };
    int i;

    system("gvim");

    for( i = 0; i < 13; i++ )
    {
        XTestFakeKeyEvent ( pDisplay, XKeysymToKeycode( pDisplay, key[i] ),
                                True, CurrentTime );
    }

    if( pDisplay == NULL ) return 1;

    XCloseDisplay(pDisplay);
    return 0;
}
Save as keystroke.c and compile with :
gcc -I/usr/X11R6/include -L/usr/X11R6/lib -o keystroke keystroke.c -lX11 -lXtst
 
Old 02-14-2007, 03:42 AM   #3
veeren
LQ Newbie
 
Registered: Mar 2004
Posts: 1

Rep: Reputation: 0
Simulating a key press

Hi guys, is there any body out there who can help me with simulating key press stuff ..I landed on this post which tells me one method on how can one simulate key press..

Problem:
I have one window that I want to close..the window can be closed if I press Escape key. Now I want to do the same thing by using a code..while I know that I can also use XcloseDisplay() routine, but using XTestFakeKeyEvent can also serve my other purposes like if I want to perform some other task by pressing some other key.

Here is what I do:
1: I search the desired window that is to be closed. I get the window id w for this target window.
2. I use XSetInputFocus() to set the focus of keyPress to window w.
3. I use XWarpPointer() to move my pointer to target window.
4. Now I use XTestFakeKeyEvent() to simulate a keypress similar to what has been suggested in email above. but nothing seems to happen. Is there any step I am missing?


Thanks in advance.
Veerendra
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ g++ wait for key press??? blizunt7 Programming 18 05-19-2014 11:09 AM
Double key press on Keyboard Zettai Linux - Newbie 2 03-09-2005 10:59 PM
scripting question press any key to continue Mr.McCormick Programming 3 02-04-2005 03:47 AM
Keyboard Problem - I have to press a key twice for certain keys daedius Linux - Hardware 4 01-13-2005 06:32 AM
Insert Boot-Disk and press any key computerlady911 Linux - Laptop and Netbook 7 10-04-2004 12:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration