Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8245471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:11:14+00:00 2026-06-07T22:11:14+00:00

I am using this code to read mouse events from the dev/input/event* in linux

  • 0

I am using this code to read mouse events from the dev/input/event* in linux .

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

#include <linux/input.h>
#include <fcntl.h>

#define MOUSEFILE "/dev/input/event4"

int main()
{
    int fd;
    struct input_event ie;

    if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
        perror("opening device");
        exit(EXIT_FAILURE);
    }

    while(read(fd, &ie, sizeof(struct input_event))) {
        printf("time %ld.%06ld\ttype %d\tcode %d\tvalue %d\n",
               ie.time.tv_sec, ie.time.tv_usec, ie.type, ie.code, ie.value);

}
    return 0;
}

It gives me the results in the format :

time 1342517261.840285 type 2 code 0 value -1

‘time’ is the timestamp, it returns the time at which the event happened.

‘code’ is event code, for example REL_X or KEY_BACKSPACE, complete
list is in include/linux/input.h.

‘value’ is the value the event carries. Either a relative change for
EV_REL, absolute new value for EV_ABS (joysticks …), or 0 for EV_KEY for
release, 1 for keypress and 2 for autorepeat.

when i click , i get the event but i don’t get the position of the mouse on the screen , what is the way to get the position of the mouse on screen .


Edit 1:So as it turns out that i have to use the relative co-ordinates to get the mouse co-ordinates .I believe this is a common requirement so there might be libraries/pre-existing code that you can use to get the co-ordinates. Any info on this topic will be very useful .


Edit2 : SOLUTION

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

#include <linux/input.h>
#include <fcntl.h>
#include <X11/Xlib.h>

#define MOUSEFILE "/dev/input/event4"

int main()
{
  int fd;
  struct input_event ie;
  Display *dpy;
  Window root, child;
  int rootX, rootY, winX, winY;
  unsigned int mask;

  dpy = XOpenDisplay(NULL);
  XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
              &rootX,&rootY,&winX,&winY,&mask);

  if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
    perror("opening device");
    exit(EXIT_FAILURE);
  }

  while(read(fd, &ie, sizeof(struct input_event))) {
    if (ie.type == 2) {
      if (ie.code == 0) {
          XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
                        &rootX,&rootY,&winX,&winY,&mask);
          //rootX += ie.value;
          }
      else if (ie.code == 1) {
          XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
                        &rootX,&rootY,&winX,&winY,&mask);
         // rootY += ie.value;
          }
      printf("time%ld.%06ld\tx %d\ty %d\n",
         ie.time.tv_sec, ie.time.tv_usec, rootX, rootY);
    } else
      printf("time %ld.%06ld\ttype %d\tcode %d\tvalue %d\n",
          ie.time.tv_sec, ie.time.tv_usec, ie.type, ie.code, ie.value);
  }
  return 0;
}

XQueryPointer seems more convenient solution . Thanks , @perreal for the guidance .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:11:16+00:00Added an answer on June 7, 2026 at 10:11 pm

    You can get the initial position from X11, and use relative coordinates to track the pointer:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #include <linux/input.h>
    #include <fcntl.h>
    #include <X11/Xlib.h>
    
    #define MOUSEFILE "/dev/input/event6"
    
    int main()
    {
      int fd;
      struct input_event ie;
      Display *dpy;
      Window root, child;
      int rootX, rootY, winX, winY;
      unsigned int mask;
    
      dpy = XOpenDisplay(NULL);
      XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
                  &rootX,&rootY,&winX,&winY,&mask); 
    
      if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
        perror("opening device");
        exit(EXIT_FAILURE);
      }
    
      while(read(fd, &ie, sizeof(struct input_event))) {
        if (ie.type == 2) {
          if (ie.code == 0) { rootX += ie.value; }
          else if (ie.code == 1) { rootY += ie.value; }
          printf("time%ld.%06ld\tx %d\ty %d\n", 
             ie.time.tv_sec, ie.time.tv_usec, rootX, rootY);
        } else if (ie.type == 1) {
          if (ie.code == 272 ) { 
            printf("Mouse button ");
            if (ie.value == 0)  
              printf("released!!\n");
            if (ie.value == 1)  
              printf("pressed!!\n");
        } else {
            printf("time %ld.%06ld\ttype %d\tcode %d\tvalue %d\n",
                ie.time.tv_sec, ie.time.tv_usec, ie.type, ie.code, ie.value);
        }
      }
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this code to read the connection string from my app.config file but
I am using this code to read values from an isolated storage IsolatedStorageFile isoStore
I am using this code to read data from a webpage : public class
I am using this code to read input stream but its not working It
I'm using this code to readmy own event Log from my win7 Computer. EventLogQuery
I am currently using this code to input data from numerous files into R:
I have been using this code to read in a file from a document
I am using this code to read data from Oracle Database. That data stored
I'm using this code: public static MjpegInputStream read(String url) { HttpResponse res; DefaultHttpClient httpclient
Using this code import re file = open('FilePath/OUTPUT.01') lines = file.read() file.close() for match

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.