Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

keyboard_Event

Posted on 1998-07-07
12
878 Views
Last Modified: 2013-12-16
I need a function that will produce a keyboard_event.

just like an user has pressed a key, but it is an other application that will do it (with that function).
(This function should be the same as keybd_event(...) in Win NT
0
Comment
Question by:tmartl
  • 6
  • 6
12 Comments
 
LVL 1

Expert Comment

by:rgmisra
ID: 1812293
Is this under X? What is the specific program?
Under X, you can try using XSendEvent, but it doesn't seem to
work with some X clients (like xterm) because the events appear
to be synthetic to the xterm program. If this is what you need,
let me know, and I'll send you a code snippet.
0
 

Author Comment

by:tmartl
ID: 1812294
it's not what i want...
thanks anyway.

what I want is the same as Keybd_event in Windows NT.
->
send a string of character to the window who has the keyboard focus.

(just like a keyboard input from a user).

N.B. : The window (that has the focus)  is not mine...
       It is from an other application...
       and my application has to simmulate a user and input a
       string in the window who has the keyboard focus.

thanks for your help.
0
 

Author Comment

by:tmartl
ID: 1812295
Yes it is under X.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Expert Comment

by:rgmisra
ID: 1812296
You can use XSendEvent to send to *other* applications' windows.
0
 

Author Comment

by:tmartl
ID: 1812297
ok.

but how can i do that?

N.B. a window can have more than one edit box and i want to put the text in the box that have the focus...

how can i have the handle of that box?
0
 
LVL 1

Accepted Solution

by:
rgmisra earned 200 total points
ID: 1812298
Well, if the box already has focus, this will work fine.
If the box doesn't have focus, I'm not sure what you'd have
to do.
Anyway, this sample generates n key events with the specified
keycode (note that X keycodes are different from character
values. you can find the keycode number with xev. The keycode
for 'q', for example, is 24.)

---------
#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>

main(int argc, char *argv[]) {
  Display *dpy = XOpenDisplay(NULL);
  int i, k, c, rev;
  Window w;
  XEvent e, b;

  if (argc < 3) {
    printf ("need 2 args, keycode and count\n");
    exit(-1);
  }

  k = atoi(argv[1]);
  c = atoi(argv[2]);

  XGetInputFocus(dpy, &w, &rev);

  for (i = 0; i < c; i++) {
    e.xkey.type = KeyPress;
    e.xkey.display = dpy;
    e.xkey.time = CurrentTime;
    e.xkey.x = e.xkey.y = 0;
    e.xkey.x_root = e.xkey.y_root = 0;
    e.xkey.state = 0;
    e.xkey.keycode = k;
    e.xkey.window = w;
    memcpy(&b, &e, sizeof(e));
    b.xkey.type = KeyRelease;
   
    if (!XSendEvent(dpy, w, True, KeyPressMask, &e)) {
      printf ("error\n");
    }
    if (!XSendEvent(dpy, w, True, KeyReleaseMask, &b)) {
      printf ("error2\n");
    }
    XSync(dpy, 0);
  }
}
---------

So run this with, for example, a.out 24 1, and it should generate
a "q" keystroke. As I mentioned earlier, this does not work w/
xterm, because it ignores synthesized events. But it works fine
for me with xv.
(You may have to "sleep" before running it, because it sends the
key to the focused window, so you need time to switch windows.
Try sleep 1; a.out 24 1, move into your running xv, and it should
quit)
0
 
LVL 1

Expert Comment

by:rgmisra
ID: 1812299
(Actually, ignore the second include. It shouldn't be
necessary.)
0
 

Author Comment

by:tmartl
ID: 1812300
I'll will try this !

How can I have the keycode of a character?

I only have the ascii code.

thank you very much!!!
0
 
LVL 1

Expert Comment

by:rgmisra
ID: 1812301
The only way I know to find keycodes is to run xev, wait
for it to stop outputting stuff, then hit the key and
read the stuff it spits out.

For my curiosity, why did you give me a "B" for my grade?
0
 

Author Comment

by:tmartl
ID: 1812302
oupss!!!  very sorry !  ;-(

I think I choose to fast.

you deserve a A++++++++++   ;-)

thanks for your help !
0
 
LVL 1

Expert Comment

by:rgmisra
ID: 1812303
no problem. just wanted to know if I did something wrong. :)
thanks.
0
 

Author Comment

by:tmartl
ID: 1812304
oupss!!!  very sorry !  ;-(

I think I choose to fast.

you deserve a A++++++++++   ;-)

thanks for your help !
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question