Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How do I count the clicks of the mouse buttons since the start of Ubuntu?

I would like to do mouse button clicks stats so I need it.

Thanks

share|improve this question

4 Answers 4

xinput can do what you need, Lukasz. You can have an one-liner (well, not exactly an one-liner, but you'll press ENTER only once :-D) for that, but first you must do some work. I swear it's pretty easy, I'll just explain most of the things here for the sake of clarity.

Open a Terminal, and type:

xinput list

You'll get a nice list of pointers, keyboard buttons, etc., like this excerpt from mine:

⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ USB+PS/2 Optical Mouse                    id=10   [slave  pointer  (2)]
⎜   ↳ PS/2 Mouse                                id=13   [slave  pointer  (2)]
⎜   ↳ AlpsPS/2 ALPS GlidePoint                  id=14   [slave  pointer  (2)]

My USB mouse is right at the second line, and you see that its ID is 10. Locate your mouse in the list and note its ID, you'll need it for the main command for what you want.

If you want to check if you got the right ID, run xinput test <id>. If you move the mouse, click buttons, etc., you should see lots of info lines going up on your Terminal. It's listing everything you do with your mouse. If nothing happens, try another id from your pointers list that makes sense to you, you probably chose the wrong one at first (this really shouldn't be that hard).

When you finally found the correct id, run this command:

xinput test <id>|sed -une '/release/ {      x
     /^$/ s/^.*$/1/
     G
     h
     s/^/      /
     s/^ *\(......\)\n/\1  /p
     x
     s/\n.*$//
     /^9*$/ s/^/0/
     s/.9*$/x&/
     h
     s/^.*x//
     y/0123456789/1234567890/
     x
     s/x.*$//
     G
     s/\n//
     h
}'

Copy the above as it is and paste it. Before you hit ENTER, replace <id> with that id you found. When you run it, you let it there while you proceed with using the system. Anytime you want to check how many clicks were registered, you just take a look at the terminal.

Some important comments:

  • Unfortunately, I'm not so good at console, shell, commands, etc. I'm pretty sure someone can improve this or provide a better solution. There's an inconvenience with my solution: you don't really have results at real time. For some reason, there seems to be buffering, and you have to generate more mouse events to read the older ones. So if you do a click and it's not shown, move your mouse a bit and soon the last click will show. Sorry about this, but I've tried some things, but don't know how to solve that yet. Hope it's not that bad for you.
  • As you see, any click is being counted. In case you want to count the clicks for only one of the buttons, replace 'release' for 'release 1', 'release 2', 'release 3', well, you got the idea. If you want to count them all, but with separate stats, run the command (with the proper button indication) in a separate terminal.

The approach I've used: xinput list <id> does the dirty job, showing everything that happens with the mouse. Then I redirect this output to sed, which will parse only lines with 'release' events. I guess it's okay to consider that a 'release' event ends the 'click process' (button down and then up). The sed script not only shows the lines with 'release', but also numbers them. This script was directly copied from info sed (section 4.7), and surely could be improved.

share|improve this answer
2  
How about: xinput test $id | awk 'BEGIN { count=0 } /release/ { count=count+1; printf "\r%-6d", count }' –  Jeremy Kerr Feb 17 '11 at 10:44
    
@Jeremy I was pretty excited to see an awk solution, but when I tried this at my system, it didn't display anything. Worked for you? Have any hints why it just kept waiting here? –  Charles Roberto Canato Feb 28 '11 at 5:37
    
awk will be buffering its input, so it may take a few events before you get the first print. –  Jeremy Kerr Mar 1 '11 at 0:55

You can use evtest provided by the linux-input project.

But you must be sure to have CONFIG_INPUT_EVBUG=y in the kernel (probably the default if you install the debug version) and set the debug level to 8

echo 8 > /proc/sys/kernel/printk

At this point, from the console, you will see several messages, that you can parse.

evbug.c: Event. Dev: gpio-keys/input0, Type: 0, Code: 0, Value: 0
evbug.c: Event. Dev: gpio-keys/input0, Type: 1, Code: 103, Value: 1

You can then install the bitbake and evtest packages

sudo apt-get install bitbake evtest

Then you can use evtest against your device, you will need some tests to find out the right event you need and you can count the occurrence of that event.

evtest /dev/input/eventXX

Otherwise, if you want to count just the click since you logged in GDM/XDM, you can develop or ask someone to do it, an application using GTK, or Qt or wxWidgets, Enlightenment, or whatever you like, to monitor mouse events and start this application as soon as the desktopm comes up.

share|improve this answer
    
This solution is a bit complicated for me, I would prefer a script or small program :( –  lukasz Dec 30 '10 at 20:37

Though not totally what you want, WhatPulse is something nice to keep track of various titbits like that + an online ranking with teams, countries, ...

share|improve this answer
up vote 0 down vote accepted

OK, I wrote a Python program to counts clicks. It runs as a daemon and saves data to the sqlite3 database. This is my first program in Python, so it may contain bugs.

https://github.com/lukaszg/kmstats

share|improve this answer
    
Your URL give me a 404 error. Check it and update your answer if necessary. –  Lucio Aug 19 '12 at 0:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.