Last active
June 24, 2024 16:45
-
-
Save javiercantero/7753445 to your computer and use it in GitHub Desktop.
A X11/Xlib program that reads the KeyPress and KeyRelease events from the window and prints them to the standard output. Used to debug the keyboard within X.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <X11/Xlib.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
Display *display; | |
Window window; | |
XEvent event; | |
int s; | |
/* open connection with the server */ | |
display = XOpenDisplay(NULL); | |
if (display == NULL) | |
{ | |
fprintf(stderr, "Cannot open display\n"); | |
exit(1); | |
} | |
s = DefaultScreen(display); | |
/* create window */ | |
window = XCreateSimpleWindow(display, RootWindow(display, s), 10, 10, 200, 200, 1, | |
BlackPixel(display, s), WhitePixel(display, s)); | |
/* select kind of events we are interested in */ | |
XSelectInput(display, window, KeyPressMask | KeyReleaseMask ); | |
/* map (show) the window */ | |
XMapWindow(display, window); | |
/* event loop */ | |
while (1) | |
{ | |
XNextEvent(display, &event); | |
/* keyboard events */ | |
if (event.type == KeyPress) | |
{ | |
printf( "KeyPress: %x\n", event.xkey.keycode ); | |
/* exit on ESC key press */ | |
if ( event.xkey.keycode == 0x09 ) | |
break; | |
} | |
else if (event.type == KeyRelease) | |
{ | |
printf( "KeyRelease: %x\n", event.xkey.keycode ); | |
} | |
} | |
/* close connection to server */ | |
XCloseDisplay(display); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@javiercantero How would I intercept a keypress on the root window? (The one that X draws everything on?)