Skip to content

Instantly share code, notes, and snippets.

@ricardosiri68
Created May 19, 2016 22:07
Show Gist options
  • Save ricardosiri68/8baae46ed63493a5f2cda553a1f0dc9e to your computer and use it in GitHub Desktop.
Save ricardosiri68/8baae46ed63493a5f2cda553a1f0dc9e to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>cuenta teclas</title>
<script language="javascript" type="text/javascript" src="script.js"></script>
</head>
<body>
<ul id="key_count">
</ul>
</body>
</html>
var Keyboardpush = function(){
this.keyCountBlck = document.getElementById('key_count');
window.addEventListener('keypress', this.pushed.bind(this), false);
};
Keyboardpush.prototype = {
keys: {},
total: 0,
pushed: function(e){
var charKey = String.fromCharCode(e.charCode);
if (!this.keys.hasOwnProperty(charKey)) {
this.keys[charKey] = 1;
} else {
this.keys[charKey]++;
}
this.total++;
this.render();
},
percent: function(value){
return (value * 100) / this.total;
},
render: function(){
var list_item, key;
this.keyCountBlck.innerHTML = '';
for(key in this.keys) {
if (this.keys.hasOwnProperty(key)) {
list_item = document.createElement('li');
this.keyCountBlck.appendChild(list_item);
list_item.innerText = key + ' ' + this.percent(this.keys[key]).toFixed(2) + '%';
}
}
}
};
window.addEventListener('load', function(){ var keypush = new Keyboardpush(); }, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment