Bucky24
May 23rd, 2009, 08:24 AM
I am trying to write a simple C program that uses XLibs to log key events on my computer (this is not for any illegal means-I am trying to write a program that does certain things when I hit keys, but does not necessarily have to be in focus).
This is the code I am using:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main(int argc, char **argv) {
char *display_name = getenv("DISPLAY");
Display *display = XOpenDisplay(display_name);
if (display == NULL) printf("bad");
XEvent *event;
XPeekEvent(display,event);
printf("type: %d\n",event->type);
if (event->type == KeyReleaseMask) {
printf("%d\n",event->xkey.keycode);
}
XCloseDisplay(display);
return 0;
}
However all this program does is enter an infinite loop (meaning that xpeekevent is finding nothing). I feel there is something missing, but there is precious little information on the web about how to use this function properly. How might I get this program to work?
Thank you for your time.
-Bucky24
This is the code I am using:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main(int argc, char **argv) {
char *display_name = getenv("DISPLAY");
Display *display = XOpenDisplay(display_name);
if (display == NULL) printf("bad");
XEvent *event;
XPeekEvent(display,event);
printf("type: %d\n",event->type);
if (event->type == KeyReleaseMask) {
printf("%d\n",event->xkey.keycode);
}
XCloseDisplay(display);
return 0;
}
However all this program does is enter an infinite loop (meaning that xpeekevent is finding nothing). I feel there is something missing, but there is precious little information on the web about how to use this function properly. How might I get this program to work?
Thank you for your time.
-Bucky24