{"id":1760,"date":"2011-06-03T09:05:05","date_gmt":"2011-06-03T09:05:05","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=1760"},"modified":"2011-06-03T09:05:05","modified_gmt":"2011-06-03T09:05:05","slug":"counting-my-key-presses","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=1760","title":{"rendered":"Counting my key presses"},"content":{"rendered":"<p><strong>Overview<\/strong><\/p>\n<p>Counting and visualising which keyboard keys I press the most often<\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/dalelane\/5792579969\/\" title=\"keypress distribution  by dalelane, on Flickr\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/farm4.static.flickr.com\/3461\/5792579969_6b529af671.jpg\" width=\"450\" height=\"230\" alt=\"keypress distribution \"\/><\/a><\/p>\n<p><strong>Background<\/strong><\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/dalelane\/5792507289\/\" title=\"the left Ctrl button by dalelane, on Flickr\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/farm3.static.flickr.com\/2803\/5792507289_47823dda71_m.jpg\" align=\"left\" hspace=\"10\" vspace=\"10\" style=\"border: thin black solid\" width=\"240\" height=\"180\" alt=\"the left Ctrl button\"\/><\/a>I noticed something yesterday. <\/p>\n<p>The lettering on my left Ctrl button is a lot more faded than the lettering on my right Ctrl button. <\/p>\n<p>I must press the left Ctrl button more often than I do the right one. <\/p>\n<p>That got me looking at the rest of the keys on the keyboard. Some of them are faded, too. <a href=\"http:\/\/www.flickr.com\/photos\/dalelane\/5792507153\/in\/photostream\/\">Some a little<\/a>, <a href=\"http:\/\/www.flickr.com\/photos\/dalelane\/5793068338\/in\/photostream\/\">some a lot<\/a>. <\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/dalelane\/5792507425\/\" title=\"the right Ctrl button by dalelane, on Flickr\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/farm3.static.flickr.com\/2026\/5792507425_d4fef987af_m.jpg\" align=\"right\" hspace=\"10\" vspace=\"10\" style=\"border: thin black solid\" width=\"240\" height=\"180\" alt=\"the right Ctrl button\"\/><\/a>I must press some of those keys more than I do others. <\/p>\n<p>I&#8217;ve played hangman, so I know that there are some <a href=\"http:\/\/en.wikipedia.org\/wiki\/Letter_frequency#Relative_frequencies_of_letters_in_the_English_language\">letters that occur more frequently in English words<\/a> than others. So I could have just said that those are the keys I probably use the most and left it at that. <\/p>\n<p>But&#8230; I don&#8217;t spend that much time writing documents or large chunks of English. <\/p>\n<p>I&#8217;m a code monkey. I&#8217;m not sure that distribution would necessarily apply to me. <\/p>\n<p>For example, I probably use the semi-colon key quite a lot &#8211; at the end of every line when writing in some languages. That wouldn&#8217;t be true for people writing English. <\/p>\n<p>So I wondered how much I use each keyboard key in comparison to each other. <\/p>\n<p>Being an obsessive compulsive geek, I couldn&#8217;t leave that as an idle wondering. I had to find out. I had to go and get the data. <\/p>\n<p><!--more--><strong>Getting the data<\/strong><\/p>\n<p>I wrote a quick-and-dirty keylogger in Python. It counts how many times I press each key, and every 15 minutes write the counts to a file. <\/p>\n<p>It&#8217;s very hacky &#8211; for example, I&#8217;ve not added a way to stop the keylogger, so I have to use <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/bb491009.aspx\">taskkill<\/a> when I want to stop it \ud83d\ude42<\/p>\n<p>Oh, and it&#8217;s Windows-specific, as I used <a href=\"http:\/\/sourceforge.net\/projects\/pyhook\/\">pyHook<\/a>. <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em; font-size: 1.1em; overflow: auto;\">import pythoncom, pyHook\r\nimport timer\r\nfrom copy import copy\r\nfrom time import sleep\r\nfrom threading import Timer\r\n\r\ncurrentCounts = {}\r\ncountsToStore = {}\r\n\r\ndef onKeyDown(event):\r\n    keyname = event.GetKey()\r\n    if keyname not in currentCounts:\r\n        currentCounts[keyname] = 1\r\n    else:\r\n        currentCounts[keyname] += 1\r\n\r\ndef storeCounts():\r\n    while True:\r\n        countsToStore = copy(currentCounts)\r\n        wordlefile = open(\"keyswordle.txt\", \"w\")\r\n        print>>wordlefile, \"key\\tcount\"\r\n        for keyname in countsToStore:\r\n            label = keyname\r\n            if keyname.startswith(\"Media_\"):\r\n                label = keyname[6:]\r\n            print>>wordlefile, label + \"\\t\" + str(countsToStore[keyname])\r\n        wordlefile.close()\r\n        countsToStore = {}\r\n        sleep(900)\r\n\r\ncaptureThread = Timer(900.0, storeCounts)\r\ncaptureThread.start()\r\n\r\nhookmgr = pyHook.HookManager()\r\nhookmgr.KeyDown = onKeyDown\r\nhookmgr.HookKeyboard()\r\npythoncom.PumpMessages()<\/pre>\n<p><strong>Visualising the data<\/strong><\/p>\n<p>I used the <a href=\"http:\/\/www.alphaworks.ibm.com\/tech\/wordcloud\">IBM Word Cloud Generator<\/a> to turn the counts into a word cloud. <\/p>\n<p>I wrote my Python script so that it would write the key-press counts to a file in a format that the word cloud generator would understand. <\/p>\n<p>To run it, I used the following config file:<\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em; font-size: 1.1em; overflow: auto;\">font: c:\/windows\/fonts\/georgiab.ttf\r\nformat: tab\r\ninputencoding: UTF-8\r\nfirstline: headings\r\nwordcolumn: key\r\nweightcolumn: count\r\nbackground: FFFFFF\r\npalette: 2367CD, 3423CD, 8923CD, CD23BC, CD2367, CD3423, CD8923, BCCD23, 67CD23, 23CD34, 23CD89, 23BCCD, 4C88E1, 81ABEA, E1A54C, EAC081\r\nplacement: HorizontalCenterLine\r\nshape: SQUARISH\r\norientation: HORIZONTAL\r\nstripnumbers: false<\/pre>\n<p>and the following command:<\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em; font-size: 1.1em; overflow: auto;\">java -jar ibm-word-cloud.jar -c config.txt -w 800 -h 600 &lt; keyswordle.txt &gt; keyswordle.png<\/pre>\n<p>And that&#8217;s pretty much it. I had a pretty word cloud to show me how much I press each key on my keyboard compared with the others. <\/p>\n<p><strong>Why did I write my own keylogger?<\/strong><\/p>\n<p>I&#8217;m a little paranoid. Googling for &#8220;keylogger&#8221; and running the first setup.exe I found didn&#8217;t feel like it&#8217;d be too safe. <\/p>\n<p>I&#8217;m sure there are trustworthy keyloggers out there, but writing a bit of Python only took me ten minutes which is probably less time than I would have spent researching ones already out there!<\/p>\n<p><strong>Did you &#8220;fix&#8221; the data?<\/strong><\/p>\n<p>I removed &#8216;Space&#8217; from the visualisation. It turns out I press the space bar a lot &#8211; making the rest of the word cloud a little difficult to interpret. <\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/dalelane\/5792580015\/\" title=\"keypress distribution with space by dalelane, on Flickr\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/farm4.static.flickr.com\/3346\/5792580015_b334a3541c.jpg\" width=\"450\" height=\"239\" alt=\"keypress distribution with space\"\/><\/a><\/p>\n<p><strong>What have I learnt doing this?<\/strong><\/p>\n<p>Erm&#8230; almost nothing. I think I learnt a depressing little bit about just how easily I can get side-tracked obsessing about trivial and unimportant details. \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Counting and visualising which keyboard keys I press the most often Background I noticed something yesterday. The lettering on my left Ctrl button is a lot more faded than the lettering on my right Ctrl button. I must press the left Ctrl button more often than I do the right one. That got me [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[498,212,497,496],"class_list":["post-1760","post","type-post","status-publish","format-standard","hentry","category-code","tag-keylogger","tag-python","tag-visualisation","tag-wordle"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1760"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1760\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}