views:

912

answers:

7

How to make a simple C program which will produce keyboard key hits.

if ( condition ) {
    KeyPress('A');
}

I am working on Ubuntu 8.10 Linux OS

+2  A: 

There is XTestFakeKeyEvent() function from Xlib.

You can USE Expect for c or C++ Programs

joe
is there any other way NOT using X11
Madni
go for Expect is one for i am using for same operation
joe
Thanks ! Will you please provide me how to use Expect, as its really new for me . Regards.
Madni
can you provide me XTestFakeKeyEvent(). I compile my c program i get an error ofbutton.c:(.text+0x9d): undefined reference to `XTestFakeKeyEvent'Any idea ?
Madni
+2  A: 

Take a look at xsendkey. The sources are included and are short, so you extract the necessary parts from it into your program.

Adrian Panasiuk
Maybe not what the OP wants, but I found it useful; thanks.
Michiel Buddingh'
A: 

Have a look at Swinput.

Swinput can fake a mouse and a keyboard by using the Linux Input System. The swinput modules read from a device and fakes hardware event (mouse motion, key presses etc) as commands written on the devices.

Lars Haugseth
Thanks ! It seems Swinput works with kernel space. I am looking for some user space application
Madni
+1  A: 

-- We can get fake keyboard events through xdotool

Example: Send the keystroke "F2" xdotool key F2

http://www.semicomplete.com/projects/xdotool/xdotool

Do any one have an idea to use in a simple C program ?

In command Line: $ xdotool key Shift+a

$ A

Also XTestFakeEvent() can also be used to send the the keyboard events

           unsigned int keycode=20;

           XTestFakeKeyEvent(dpy, keycode, 1, 0/*CurrentTime*/);

when I compile my c program i get an error of,

button.c:(.text+0x9d): undefined reference to `XTestFakeKeyEvent'

I have to link it during compilation as -lXtst, but is with no sucess

Any idea ? Regards,

Madni
A: 

Get Fake Key Events by Xdotool

CODE

//Compile As: gcc button.c -lX11

include < X11/Xlib.h >

include < X11/Xutil.h >

include < stdio.h >

include < X11/extensions/XTest.h >

void press_button() {
Display *d;

d = XOpenDisplay(NULL);

    if(d == NULL)
    {

        //fprintf(stderr, "Errore nell'apertura del Display !!!\n");

        //exit(0);
    }

system("xdotool key Shift+a");

XFlush(d);

    XCloseDisplay(d);

}

int main(){

press_button();

return 0;

}

Madni
A: 

Although this is not C, you can produce key hits in Java very easily:

import java.awt.Robot;
import java.awt.AWTException;
import java.awt.event.KeyEvent;


public class key
{
    public static void main(String args[])
    {
        try {
            Robot r = new Robot();
            r.delay(2000);
            r.keyPress(KeyEvent.VK_W);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
};
Adrian Panasiuk
+1  A: 

Here's a simple example using libxdo (from xdotool). (Caveat: I am the xdotool author)

 /* File: testkey.c
 *
 * Compile with:
 * gcc -lxdo testkey.c
 *
 * Requires libxdo (from xdotool project)
 */

#include <xdo.h>

int main() {
  xdo_t *xdo = xdo_new(NULL);
  xdo_keysequence(xdo, CURRENTWINDOW, "A", 0);
  return  0;
}
Jordan Sissel