How do I count the clicks of the mouse buttons?

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

3 Answers

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, anyclick 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.

3

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.

1

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like