Knowledge Base Nr: 00129 kbstick.c - http://www.swe-kaiser.de

Downloads:

Linux: Tastendrücke unter X simulieren/schicken

  
//g++ kbstick.c -o kbstick -lncurses -I/usr/X11R6/include -L/usr/X11R6/lib -lX11 -lXtst

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

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

#include <signal.h>

Display* ourXDisplay = 0;

const int delayForSend = 10; /* Delay in mSec for sending key msgs */


Display* getXDisplay(const char* appName) {

int ourEvent, ourError, ourMajor, ourMinor;

Display* ourDisplay = XOpenDisplay(0);

if (! ourDisplay) {
printf("\n ERROR: This program is made to be run in X Windows.\n\n");
exit(1);
}
if (! XTestQueryExtension(ourDisplay, &ourEvent, &ourError, &ourMajor, &ourMinor) ) {
printf("%s: ", appName);
printf("XServer %s doesn't support the Xtest extensions we need...\n", DisplayString(ourDisplay));
XCloseDisplay(ourDisplay);
exit(1);
}
printf("\nServer %s supports XTest (version %d.%d). Good.\n\n", DisplayString(ourDisplay), ourMajor, ourMinor);

XSync( ourDisplay, True);

return ourDisplay;
}


void doXKey(Display* ourDisplay, const int keyToSend)
{
XTestFakeKeyEvent(ourDisplay, (unsigned int)keyToSend, True, delayForSend);
XTestFakeKeyEvent(ourDisplay, (unsigned int)keyToSend, False, delayForSend);
}


void doShiftXKey(Display* ourDisplay, const int keyToSend)
{
XTestFakeKeyEvent(ourDisplay, (unsigned int)63, True, delayForSend); //Shift_L
XTestFakeKeyEvent(ourDisplay, (unsigned int)keyToSend, True, delayForSend);
XTestFakeKeyEvent(ourDisplay, (unsigned int)keyToSend, False, delayForSend);
XTestFakeKeyEvent(ourDisplay, (unsigned int)63, False, delayForSend); //Shift_L
}


void doAltXKey(Display* ourDisplay, const int keyToSend)
{
XTestFakeKeyEvent(ourDisplay, (unsigned int)77, True, delayForSend); //Alt_L
XTestFakeKeyEvent(ourDisplay, (unsigned int)keyToSend, True, delayForSend);
XTestFakeKeyEvent(ourDisplay, (unsigned int)keyToSend, False, delayForSend);
XTestFakeKeyEvent(ourDisplay, (unsigned int)77, False, delayForSend); //Alt_L
}


void doCtrlXKey(Display* ourDisplay, const int keyToSend)
{
XTestFakeKeyEvent(ourDisplay, (unsigned int)76, True, delayForSend); //Control_L
XTestFakeKeyEvent(ourDisplay, (unsigned int)keyToSend, True, delayForSend);
XTestFakeKeyEvent(ourDisplay, (unsigned int)keyToSend, False, delayForSend);
XTestFakeKeyEvent(ourDisplay, (unsigned int)76, False, delayForSend); //Control_L
}


void cleanup(const int signal)
{
XCloseDisplay(ourXDisplay);
printf("\nExiting on signal.\n");
exit(0);
}


int main(int argc, char *argv[])
{
printf("\nKBStick v0.7 GPL (c) 2000 by David A. Fulgham\n");

ourXDisplay = getXDisplay(argv[0]);

signal(SIGINT, cleanup);
signal(SIGTERM, cleanup);

sleep(2);

char szKeysym[50];

strcpy(szKeysym, argv[1]);

KeyCode kc;
KeySym ks;

ks = XStringToKeysym(szKeysym);
if( ks == NoSymbol )
{
printf("Unable to resolve keysym for '%s'\n", szKeysym);
exit (0);
}

kc = XKeysymToKeycode(ourXDisplay, ks );
printf("String '%s' maps to keysym '%d'\n", szKeysym, ks );
printf("String '%s' maps to keycode '%d'\n", szKeysym, kc );

//doXKey(ourXDisplay, kc);
//doShiftXKey(ourXDisplay, kc);
//doAltXKey(ourXDisplay, kc);

doCtrlXKey(ourXDisplay, kc);

// doXKey(ourXDisplay, XK_B);
// doXKey(ourXDisplay, XK_Z);
// doXKey(ourXDisplay, XK_0);
// doXKey(ourXDisplay, XK_1);
// doXKey(ourXDisplay, XK_9);
// doXKey(ourXDisplay, XK_F1);

sleep(2);

XCloseDisplay(ourXDisplay);

printf("\nExiting normally.\n\n");
exit(0);
}