Posted By

xyzeugene on 11/18/17


Tagged

c mouse linux


Versions (?)

C Program to read mouse in console via GPM


 / Published in: Pascal
 

URL: https://www.linuxquestions.org/questions/linux-software-2/capture-or-monitor-mouse-button-events-in-console-703018/

Install GPM using installer such as apt or dnf

All you have to do is save code with mouse.c file name and compile it : gcc -o mouse mouse.c -lgpm

and you got a "mouse" program that executes l.sh on left, r.sh on right and m.sh on middle mouse button click.

You will also have to install gpm daemon (and run it) and libgpm.

There is one issue, I can't get mouse wheel work whit libgpm. libgpm have very poor documentary so maybe someone knows how to read wheel events?

  1. In case somebody is interested in answer I got it with this C program:
  2.  
  3. #include <stdio.h>
  4. #include <gpm.h>
  5.  
  6. int my_handler(Gpm_Event *event, void *data)
  7. { if(event->type & GPM_DOWN)
  8. { if(event->buttons & GPM_B_LEFT)
  9. system("./l.sh");
  10. if(event->buttons & GPM_B_RIGHT)
  11. system("./r.sh");
  12. if(event->buttons & GPM_B_MIDDLE)
  13. system("./m.sh");
  14. }
  15. return 0;
  16. }
  17.  
  18. int main()
  19. { Gpm_Connect conn;
  20. int c;
  21. conn.eventMask = ~0; /* Want to know about all the events */
  22. conn.defaultMask = 0; /* don't handle anything by default */
  23. conn.minMod = 0; /* want everything */
  24.  
  25. if(Gpm_Open(&conn, 0) == -1)
  26. printf("Cannot connect to mouse server\n");
  27. gpm_handler = my_handler;
  28. while((c = Gpm_Getc(stdin)) != EOF)
  29. Gpm_Close();
  30. return 0;
  31. }
  32. /*EOF*/

Report this snippet  

You need to login to post a comment.