How to simulate a keypress

From ThorstensHome

Jump to: navigation, search
Home < Tutorials < C Programming Tutorial < How to simulate a keypress

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 );
}
Personal tools