Tutorials/C Programming Tutorial/How to simulate a keypress

From ThorstensHome
Revision as of 17:03, 28 July 2007 by WikiSysop (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The following program simulates the keypress of NUMLOCK in your session of the X Windowing System.

#include <X11/extensions/XTest.h>
#include <X11/keysym.h>

/* Compile this using
g++ -lXtst main.cpp
*/

int main()
{
  Display* display= XOpenDisplay(NULL);
  XTestFakeKeyEvent(display, XKeysymToKeycode( display, XK_Num_Lock ), true, CurrentTime);
  XTestFakeKeyEvent(display, XKeysymToKeycode( display, XK_Num_Lock ), false, CurrentTime);
  XCloseDisplay( display );
}