From Wikipedia, the free encyclopedia
/*
This program changes the keysyms associated with the
keycode that is currently associated to F1 to "a b c d",
wait for 20 seconds and then switches back to the normal
association. The function for switching back to the old
association is also called on SIGINT, SIGQUIT, and SIGTERM.
Pressing F1 during the 20 seconds sleep produces the
character "a". The other characters are produced according
to a combination of factors. In Linux, the second character
is obtained by pressing Control-Alt-F1. The third and fourth
require a keycode to be associated to the keysym Mode_switch
and that this keycode is also added as a modifer in
Mod1-Mod5.
*/
#include<X11/Xlib.h>
#include<signal.h>
#include<X11/keysym.h>
void printMapping(KeySym *v, int knum) {
int i;
// printf("knum=%d\n", knum);
for(i=0; i<knum; i++)
if(v[i]==NoSymbol)
printf("NoSymbol ");
else
printf("0x%04X ", v[i]);
printf("\n");
}
Display *d;
KeySym *orig;
int knum;
KeyCode f1;
void restore(int s) {
printf("Restoring mapping.\n");
XChangeKeyboardMapping(d, 0x43, 4, orig, 1);
XCloseDisplay(d);
exit(0);
}
int main(int argn, char *argv[]) {
KeySym new[4]={XK_a, XK_b, XK_c, XK_d};
KeySym *temp;
int ntemp;
int add;
/* your computer will crush and burn... */
printf("This program changes the assignment of characters to the F1 key.\n");
printf("After execution that key *might* not be usable. Continue (y/n)? ");
if(getchar()!='y')
return 0;
/* open connection with the server */
d=XOpenDisplay(NULL);
if(d==NULL) {
printf("Cannot open display\n");
exit(1);
}
/* get keycode of the F1 key */
f1=XKeysymToKeycode(d, XK_F1);
printf("Keycode of f1=0x%02X\n", f1);
if(f1==0) {
printf("No keycode is associated to F1, change not done\n");
exit(1);
}
/* get modifier mapping */
printf("Current mapping: ");
orig=XGetKeyboardMapping(d, f1, 1, &knum);
printMapping(orig, knum);
/* register signal handler (restore state) */
signal(SIGTERM, restore);
signal(SIGQUIT, restore);
signal(SIGINT, restore);
/* change modifier */
XChangeKeyboardMapping(d, f1, 4, new, 1);
printf("Changed mapping.\n");
printf("Current mapping: ");
temp=XGetKeyboardMapping(d, f1, 1, &ntemp);
printMapping(temp, ntemp);
/* sleep and change back */
sleep(20);
restore(0);
XCloseDisplay(d);
return 0;
}