LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-30-2015, 06:16 AM   #1
browny_amiga
Member
 
Registered: Dec 2001
Location: /mnt/UNV/Mlkway/Earth/USA/California/Silicon Valley
Distribution: Kubuntu, Debian Buster Stable, Windoze 7
Posts: 684

Rep: Reputation: 56
Smile Global Keystroke counter?


Hi,

I type a lot, a TON and am curious how much, so I thought it might be very interesting to have a tool count the total keystrokes I do every day on my linux laptop. Is there a tool that can do that? If not, how can I build it myself? It would need to be global though, meaning all keystrokes, not just when a program has focus (and that has been the problem so far)

Thanks for any pointers,

Markus
 
Old 03-30-2015, 12:12 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I don't know of any tools and besides adding code to the keyboard driver and rebuilding your kernel, you can look at the /dev/input/ tree. For instance mine has a by-id subdirectory where it has links showing that /dev/input/event3 is one of my keyboard driver files. Turns out if I do "sudo cat /dev/input/event3" it shows me a bunch of binary stuff. I can also do "suco hexdump /dev/input/event3" and then it shows definitive packets where 3-4 of them come out with each keystroke, probably some indicating the down key and some indicating the key release. You could either determine what that entire protocol is, or instead figure out if it's always exactly 3 or 4 records per keystroke and then write a program to look at this file and count the number of records transcended over a certain time.

If I were in your shoes I'd write a program to measure that, see how much I did type, and then send a post out to the forum challenging people to exceed you, that is without cheating and distorting the rules.
 
1 members found this post helpful.
Old 03-30-2015, 01:47 PM   #3
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Try:
Code:
#include <linux/input.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>

static uint64_t count;
static int fd;

void termination_handler (int signum)
{
	printf ("Characters typed = %lu\n", count);

	// closed
	close (fd);

	_exit (0);
}

int main (int argc, char *argv[])
{
	if (argc < 2)
	{
		fprintf (stderr, "Usage:\n %s <device>\n", argv[0]);
		return 1;
	}

	fd = open (argv[1], O_RDONLY); // ! closed
	if (-1 == fd)
	{
		fprintf (stderr, "ERROR: Cannot open %s\n", argv[1]);
		return 1;
	}

	struct input_event ev;

	struct sigaction sa;
	sa.sa_handler = termination_handler;
	sigemptyset (&sa.sa_mask);
	sa.sa_flags = 0;
	sigaction (SIGINT, &sa, NULL);

	while (read(fd, &ev, sizeof(struct input_event)) != -1)
	{
		if (ev.type == 1 && ev.value != 0)
		{
			count++;
		}
	}

	return 0;
}
Check 'cat /proc/bus/input/devices' for input device nodes for your keyboard, it will usually be something like '/dev/input/event3' with a different number than 3. It will print the number of characters typed when you press Ctrl-C.
 
1 members found this post helpful.
Old 05-12-2015, 03:52 AM   #4
browny_amiga
Member
 
Registered: Dec 2001
Location: /mnt/UNV/Mlkway/Earth/USA/California/Silicon Valley
Distribution: Kubuntu, Debian Buster Stable, Windoze 7
Posts: 684

Original Poster
Rep: Reputation: 56
Complicated.... but workable. Thanks for the pointers!
 
  


Reply



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
LXer: Counter-Strike: Global Offensive Officially Lands on Linux, Skips Beta LXer Syndicated Linux News 0 09-23-2014 11:12 AM
LXer: Counter Strike Global Offensive Linux Beta Due Soon! LXer Syndicated Linux News 0 09-22-2014 10:42 PM
LXer: Counter-Strike: Global Offensive to Finally Arrive on Linux LXer Syndicated Linux News 0 09-11-2014 07:11 AM
LXer: Btrfs & Counter-Strike: Global Offensive LXer Syndicated Linux News 0 03-23-2012 07:20 PM
LXer: OpenClinica Global Conference to Bring Together Global Community for Open Sourc LXer Syndicated Linux News 0 01-08-2010 10:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:53 AM.

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