r/programmingrequests • • Mar 29 '16

[Request] Making a script that, when I press a key, it records exactly when I pressed it, and totals how many times I pressed it in a given time interval.

Ideally, I could do this in my browser, like Chrome or Firefox. If this concept already exists in some easily-used fashion, maybe someone could point me that way? =)

I'm attempting to monitor frequency of a given event, as well as totaling how many times that event has occurred in an arbitrarily-defined time frame.

So, say I'm watching an experiment that starts at 3:00 PM and runs until 4:00 PM (or some arbitrarily-defined time), and I'm looking out for some event that I will track by hand by pressing the Spacebar on my keyboard or something.

At the end, I'd like an output that tells me how many times I pressed my button in that time interval, and maybe an average "# of events per minute" or something. Ideally I'd just get a data dump of the event and its associated time stamp starting from when I started running the script, and I can try and figure out other data from it.

Thanks! =D

4 Upvotes

3 comments sorted by

4

u/LenAnderson Mar 30 '16

Save the following code as an .html file, open it and start pressing keys... Best to use Chrome as Firefox does not have a nice direct text representation for all keys.

The counting table and the raw log should allow for copy-pasting into Excel or similar applications.

<html>
<head>
    <title>Key Counter</title>
    <style>
        body {
            font-family: sans-serif;
            text-align: center;
        }
        .num { text-align: right; }
        #stats, #log {
            display: inline-block;
            margin: 0 20px;
            text-align: left;
            vertical-align: top;
        }
    </style>
</head>
<body>
    <h1>Key Counter</h1>
    <table id="stats"><tbody></tbody></table>
    <pre id="log"></pre>
    <script>
        var data = [];
        var start = new Date().getTime();
        var stats = document.querySelector('#stats > tbody');
        var log = document.querySelector('#log');

        addEventListener('keydown', function(evt) {
            evt.preventDefault();
            var key = evt.key.trim() || evt.code;
            data.push({key: key, time: new Date()});
            updateStats();
            updateLog();
        });
        
        function updateStats() {
            var time = (new Date().getTime() - start) / 1000 / 60;
            var count = {};
            data.forEach(function(it) {
                if (!count[it.key]) count[it.key] = 0;
                count[it.key]++;
            });
            while (stats.children.length > 0) stats.children[0].remove();
            Object.keys(count).sort().forEach(function(key) {
                var tr = document.createElement('tr');
                var tdKey = document.createElement('td');
                tdKey.textContent = key;
                tr.appendChild(tdKey);
                var tdCount = document.createElement('td');
                tdCount.textContent = count[key];
                tdCount.classList.add('num');
                tr.appendChild(tdCount);
                var tdFreq = document.createElement('td');
                tdFreq.textContent = Math.round(count[key] / time) + '/min';
                tdFreq.classList.add('num');
                tr.appendChild(tdFreq);
                stats.appendChild(tr);
            });
        }
        
        function updateLog() {
            log.innerHTML = '';
            log.appendChild(document.createTextNode(data.map(function(it) { return it.time.toISOString() + '\t' + it.key; }).join('\n')));
        }
    </script>
</body>

1

u/spiralingmadness Mar 30 '16

This... this is wonderful.

I appreciate the time and effort you must've taken to make this; Thank you, so so much!

2

u/NoG5 May 01 '16

I was interested in this also so i made this but it is a desktop app so it can logg in the backgrund while the user play a game or what ever image, download