Wednesday, April 10, 2013

Simulating Key Events in C using XWindow Programming

lets write a small program, where we can simulate ALT+F4(close) key functionality in C using Xwindow System Programming.

// FileName: CloseSimulation.c

#include <X11/Xlib.h>
#include  <X11/keysym.h>
#include  <X11/extensions/XTest.h>
#include  <stdio.h>
void main()
    Display *display;
    unsigned int keycode[2];
    display = XOpenDisplay(NULL);
    //getting Alt and F4 key codes
    keycode[0] = XKeysymToKeycode(display, XK_Alt_L);
    keycode[1] = XKeysymToKeycode(display, XK_F4);
    //Pressing Alt
    XTestFakeKeyEvent(display, keycode[0], True, 0);
    //Pressing F4
    XTestFakeKeyEvent(display, keycode[1], True, 0);
    //Releasing F4
    XTestFakeKeyEvent(display, keycode[1], False, 0);
    //Releasing Alt
    XTestFakeKeyEvent(display, keycode[0], False, 0);

To Run: gcc -lX11 -lXtst CloseSimulation.c

Output: It simply simulates Alt+F4 and closes the terminal where you are running this program.

To Know executing commands through C program Click Here

No comments:

Post a Comment