Useless Things Series: Mouse and Keyboard Event Counters

Counts mouse movement in inches, scroll events, key presses, mouse clicks (left and right), and displays the top 5 keys pressed along with their counts.

  1. // ==UserScript==
  2. // @name Useless Things Series: Mouse and Keyboard Event Counters
  3. // @version 1.3
  4. // @description Counts mouse movement in inches, scroll events, key presses, mouse clicks (left and right), and displays the top 5 keys pressed along with their counts.
  5. // @match *://*/*
  6. // @grant none
  7. // @license MIT
  8. // @namespace https://greasyfork.org/users/1126616
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. var mouseInches = 0;
  13. var scrollCount = 0;
  14. var letterCount = 0;
  15. var leftClickCount = 0;
  16. var rightClickCount = 0;
  17. var keyPressCount = {};
  18.  
  19. var countersDiv = document.createElement('div');
  20. countersDiv.style.position = 'fixed';
  21. countersDiv.style.left = '10px';
  22. countersDiv.style.top = '10px';
  23. countersDiv.style.padding = '10px';
  24. countersDiv.style.border = '2px solid #fff';
  25. countersDiv.style.borderRadius = '5px';
  26. countersDiv.style.transition = 'background-color 0.3s, opacity 0.3s';
  27. countersDiv.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
  28. countersDiv.style.color = '#fff';
  29. countersDiv.style.cursor = 'move';
  30. countersDiv.style.userSelect = 'none';
  31. countersDiv.style.display = 'none';
  32. countersDiv.style.opacity = '0';
  33. document.body.appendChild(countersDiv);
  34.  
  35. var mouseCounter = createCounter('Mouse Inches');
  36. var scrollCounter = createCounter('Scrolls');
  37. var letterCounter = createCounter('Letters Pressed');
  38. var leftClickCounter = createCounter('Left Clicks');
  39. var rightClickCounter = createCounter('Right Clicks');
  40. var topKeysCounter = createCounter('Top 5 Keys');
  41.  
  42. countersDiv.appendChild(mouseCounter);
  43. countersDiv.appendChild(scrollCounter);
  44. countersDiv.appendChild(letterCounter);
  45. countersDiv.appendChild(leftClickCounter);
  46. countersDiv.appendChild(rightClickCounter);
  47. countersDiv.appendChild(topKeysCounter);
  48.  
  49. var timeout;
  50. var lastScrollTime = new Date().getTime();
  51. var isDragging = false;
  52. var offsetX, offsetY;
  53.  
  54. countersDiv.addEventListener('mousedown', function(e) {
  55. isDragging = true;
  56. offsetX = e.clientX - countersDiv.getBoundingClientRect().left;
  57. offsetY = e.clientY - countersDiv.getBoundingClientRect().top;
  58. });
  59.  
  60. document.addEventListener('mousemove', function(e) {
  61. if (isDragging) {
  62. countersDiv.style.left = e.clientX - offsetX + 'px';
  63. countersDiv.style.top = e.clientY - offsetY + 'px';
  64. }
  65. });
  66.  
  67. document.addEventListener('mouseup', function() {
  68. isDragging = false;
  69. });
  70.  
  71. document.addEventListener('mousemove', function(event) {
  72. clearTimeout(timeout);
  73.  
  74. var deltaX = event.movementX || 0;
  75. var deltaY = event.movementY || 0;
  76. mouseInches += Math.sqrt(deltaX * deltaX + deltaY * deltaY) / 96;
  77. updateCounters();
  78.  
  79. timeout = setTimeout(function() {
  80. countersDiv.style.opacity = '0';
  81. setTimeout(function() {
  82. countersDiv.style.display = 'none';
  83. }, 300);
  84. }, 3000);
  85. });
  86.  
  87. document.addEventListener('wheel', function(event) {
  88. var currentTime = new Date().getTime();
  89. if (currentTime - lastScrollTime > 100) {
  90. scrollCount++;
  91. lastScrollTime = currentTime;
  92. }
  93.  
  94. updateCounters();
  95. });
  96.  
  97. document.addEventListener('keydown', function(event) {
  98. letterCount++;
  99. var key = event.key.toUpperCase();
  100. keyPressCount[key] = (keyPressCount[key] || 0) + 1;
  101.  
  102. updateCounters();
  103. });
  104.  
  105. document.addEventListener('mousedown', function(event) {
  106. if (event.button === 0) {
  107. leftClickCount++;
  108. } else if (event.button === 2) {
  109. rightClickCount++;
  110. }
  111.  
  112. updateCounters();
  113. });
  114.  
  115. function updateCounters() {
  116. mouseCounter.textContent = 'Mouse Inches: ' + mouseInches.toFixed(2) + ' in';
  117. scrollCounter.textContent = 'Scrolls: ' + scrollCount;
  118. letterCounter.textContent = 'Letters Pressed: ' + letterCount;
  119. leftClickCounter.textContent = 'Left Clicks: ' + leftClickCount;
  120. rightClickCounter.textContent = 'Right Clicks: ' + rightClickCount;
  121.  
  122. // Display top 5 keys pressed in a column
  123. var sortedKeys = Object.keys(keyPressCount).sort(function(a, b) {
  124. return keyPressCount[b] - keyPressCount[a];
  125. });
  126.  
  127. var topKeysText = 'Top 5 Keys:<br>';
  128. for (var i = 0; i < Math.min(5, sortedKeys.length); i++) {
  129. var key = sortedKeys[i];
  130. topKeysText += key + ': ' + keyPressCount[key] + '<br>';
  131. }
  132.  
  133. topKeysCounter.innerHTML = topKeysText;
  134.  
  135. countersDiv.style.display = 'block';
  136. countersDiv.style.opacity = '1';
  137. }
  138.  
  139. function createCounter(label) {
  140. var counter = document.createElement('div');
  141. counter.style.marginBottom = '8px';
  142. counter.textContent = label + ': 0';
  143. counter.style.color = '#fff';
  144. return counter;
  145. }
  146. })();