Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-12-2007, 02:12 AM   #1
Registered: Jun 2003
Distribution: Ubuntu, Debian
Posts: 40

Rep: Reputation: 15
Unhappy Using Xlib to monitor mouse and keyboard

Hi. I want to write a program using Xlib to monitor user activities including mouse and keyboard. I wrote the program. The strange thing is it only monitor part of the events.

I used XSelectInput to monitor several types of event. But only SubstructureNotifyMask events are displayed. Can anyone please help me why?? I can't realy figure it out.

Thanks in advance!!!

#include <X11/Xlib.h>

#include <stdio.h>
#include <stdlib.h>		/* getenv(), etc. */
#include <unistd.h>		/* sleep(), etc.  */

int main(int argc, char* argv[]) {
	Display* display;		/* pointer to X Display structure.           */
	int screen_num;		/* number of screen to place the window on.  */
	char *display_name = getenv("DISPLAY");  /* address of the X display.      */
	XEvent an_event;
	Window root_window;
	unsigned long events;
	XWindowAttributes attrs;

	static int count = 0;

	display = XOpenDisplay(display_name);
	if (display == NULL) {
		fprintf(stderr, "%s: cannot connect to X server '%s'\n",
				argv[0], display_name);

	screen_num = DefaultScreen(display);

	root_window = RootWindow(display, screen_num);

	XSync(display, False);

	XGetWindowAttributes (display, root_window, &attrs);
	XSelectInput(display, root_window, SubstructureNotifyMask | PointerMotionMask | KeyPressMask);
	while(1) {
		XNextEvent(display, &an_event);
		switch(an_event.type) {
		  case Expose:
 			//printf("called Expose event(%d)\n", count);
 			//printf("called default(%d)\n", count);
		printf("event.type: %d, (%d)\n", an_event.type, count);


	/* close the connection to the X server. */
	return 0;
Old 06-13-2007, 01:31 PM   #2
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,811

Rep: Reputation: 493Reputation: 493Reputation: 493Reputation: 493Reputation: 493
Perhaps looking at the sources for the xev program would shed some light(I mean plagiarized code) on the matter.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Laptop monitor/keyboard/mouse - adapt to desktop? jantman Linux - Hardware 4 02-05-2007 12:48 AM
how do I stop monitor, keyboard and mouse? totally stumped! andyroles Linux - Networking 6 06-27-2006 07:02 PM
Xlib: Hide mouse cursor gurra Programming 0 10-07-2003 06:21 AM
Keyboard Mouse Monitor Detection Sathe Linux - General 4 01-17-2002 06:10 PM
booting without mouse, keyboard, monitor jonfa Linux - General 1 05-27-2001 01:49 PM

All times are GMT -5. The time now is 11:09 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration