Key press visual representation

Shows what keys you are hitting, **press escape to open the menu** Can be useful for .io game streaming, tutorials, etc.

  1. // ==UserScript==
  2. // @name Key press visual representation
  3. // @namespace http://tampermonkey.net/
  4. // @version 5
  5. // @description Shows what keys you are hitting, **press escape to open the menu** Can be useful for .io game streaming, tutorials, etc.
  6. // @author MrBlank
  7. // @match *://lolbeans.io/*
  8. // @match *://*.moomoo.io/*
  9. // @match *://moomoo.io/*
  10. // @match *://sandbox.moomoo.io/*
  11. // @match *://dev.moomoo.io/*
  12. // @match *://*.smashkarts.io/*
  13. // @match *://*.sploop.io/*
  14. // @match *://*.yohoho.io/*
  15. // @match *://*.brutal.io/*
  16. // @match *://*.bonk.io/*
  17. // @match *://*.florr.io/*
  18. // @match *://*.copter.io/*
  19. // @match *://*.defly.io/*
  20. // @grant GM_setValue
  21. // @grant GM_getValue
  22. // @run-at document-end
  23. // @icon https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT3qlribLz6iUSvZNdelROWb7FlW5yGDJGgog&s
  24. // ==/UserScript==
  25.  
  26. //PRESS ESCAPE TO OEPN THE MENU
  27.  
  28. (function() {
  29. 'use strict';
  30.  
  31. var styles = `
  32. .key-press-menu {
  33. position: fixed;
  34. top: 20px;
  35. right: 20px;
  36. background-color: #f0f0f0;
  37. border-radius: 8px;
  38. box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  39. padding: 15px;
  40. font-family: Arial, sans-serif;
  41. z-index: 10000;
  42. display: none;
  43. resize: both;
  44. overflow: auto;
  45. min-width: 200px;
  46. min-height: 150px;
  47. max-width: 600px; /* Restrict max width */
  48. max-height: 400px; /* Restrict max height */
  49. }
  50. .key-press-menu h3 {
  51. margin-top: 0;
  52. color: #333;
  53. }
  54. .key-press-menu input, .key-press-menu button, .key-press-menu select {
  55. margin: 5px 0;
  56. padding: 5px;
  57. width: 100%;
  58. box-sizing: border-box;
  59. }
  60. .key-press-menu button {
  61. background-color: #4CAF50;
  62. color: white;
  63. border: none;
  64. padding: 10px;
  65. text-align: center;
  66. text-decoration: none;
  67. display: inline-block;
  68. font-size: 14px;
  69. margin: 4px 2px;
  70. cursor: pointer;
  71. border-radius: 4px;
  72. }
  73. .key-box {
  74. position: fixed;
  75. width: 50px;
  76. height: 50px;
  77. background-color: transparent;
  78. border: 2px solid #333;
  79. text-align: center;
  80. line-height: 50px;
  81. font-family: Arial, sans-serif;
  82. font-weight: bold;
  83. color: #333;
  84. cursor: move;
  85. user-select: none;
  86. border-radius: 5px;
  87. box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  88. }
  89. `;
  90.  
  91. // Add styles to the document
  92. var styleElement = document.createElement('style');
  93. styleElement.textContent = styles;
  94. document.head.appendChild(styleElement);
  95.  
  96. function createMenu() {
  97. var menuContainer = document.createElement('div');
  98. menuContainer.className = 'key-press-menu';
  99. menuContainer.innerHTML = `
  100. <h3>Key Press Settings</h3>
  101. <input type="text" id="keyInput" placeholder="Enter key">
  102. <input type="color" id="colorInput" value="#ff0000">
  103. <button id="addKeyButton">Add Key</button>
  104. <select id="removeKeySelect"></select>
  105. <button id="removeKeyButton">Remove Selected Key</button>
  106. `;
  107. document.body.appendChild(menuContainer);
  108.  
  109. // Add key
  110. document.getElementById('addKeyButton').addEventListener('click', function() {
  111. var key = document.getElementById('keyInput').value.toUpperCase();
  112. var color = document.getElementById('colorInput').value;
  113. if (key && color) {
  114. addKeyBox(key, color, {top: 100, left: 100});
  115. document.getElementById('keyInput').value = '';
  116. saveKeys();
  117. updateRemoveKeySelect();
  118. }
  119. });
  120.  
  121. // Remove key
  122. document.getElementById('removeKeyButton').addEventListener('click', function() {
  123. var select = document.getElementById('removeKeySelect');
  124. var selectedKey = select.value;
  125. if (selectedKey) {
  126. removeKeyBox(selectedKey);
  127. saveKeys();
  128. updateRemoveKeySelect();
  129. trackRemovedKey(selectedKey);
  130. }
  131. });
  132. }
  133.  
  134. function toggleMenu() {
  135. var menuContainer = document.querySelector('.key-press-menu');
  136. if (menuContainer.style.display === 'none' || menuContainer.style.display === '') {
  137. menuContainer.style.display = 'block';
  138. } else {
  139. menuContainer.style.display = 'none';
  140. }
  141. }
  142.  
  143. document.addEventListener('keydown', function(event) {
  144. if (event.key === 'Escape') {
  145. toggleMenu();
  146. }
  147. });
  148.  
  149. function addKeyBox(key, color, pos) {
  150. var box = document.createElement('div');
  151. box.className = 'key-box';
  152. box.textContent = key;
  153. box.dataset.color = color;
  154. box.style.left = `${pos.left}px`;
  155. box.style.top = `${pos.top}px`;
  156. if (key === 'SPACE') {
  157. box.style.width = '200px';
  158. box.style.height = '50px';
  159. }
  160. document.body.appendChild(box);
  161. makeDraggable(box);
  162. }
  163.  
  164. function removeKeyBox(key) {
  165. var keyBoxes = document.querySelectorAll('.key-box');
  166. keyBoxes.forEach(function(box) {
  167. if (box.textContent === key) {
  168. document.body.removeChild(box);
  169. }
  170. });
  171. }
  172.  
  173. function makeDraggable(element) {
  174. var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  175. element.onmousedown = dragMouseDown;
  176.  
  177. function dragMouseDown(e) {
  178. e = e || window.event;
  179. e.preventDefault();
  180. pos3 = e.clientX;
  181. pos4 = e.clientY;
  182. document.onmouseup = closeDragElement;
  183. document.onmousemove = elementDrag;
  184. }
  185.  
  186. function elementDrag(e) {
  187. e = e || window.event;
  188. e.preventDefault();
  189. pos1 = pos3 - e.clientX;
  190. pos2 = pos4 - e.clientY;
  191. pos3 = e.clientX;
  192. pos4 = e.clientY;
  193. element.style.top = `${element.offsetTop - pos2}px`;
  194. element.style.left = `${element.offsetLeft - pos1}px`;
  195. }
  196.  
  197. function closeDragElement() {
  198. document.onmouseup = null;
  199. document.onmousemove = null;
  200. saveKeys();
  201. }
  202. }
  203.  
  204. function saveKeys() {
  205. var keys = [];
  206. document.querySelectorAll('.key-box').forEach(function(box) {
  207. keys.push({
  208. key: box.textContent,
  209. color: box.dataset.color,
  210. top: parseInt(box.style.top),
  211. left: parseInt(box.style.left)
  212. });
  213. });
  214. localStorage.setItem('keyPressVisualKeys', JSON.stringify(keys));
  215. }
  216.  
  217. function loadKeys() {
  218. var storedKeys = JSON.parse(localStorage.getItem('keyPressVisualKeys')) || [];
  219. var removedKeys = JSON.parse(localStorage.getItem('removedKeys')) || [];
  220. var existingKeys = new Set(storedKeys.map(item => item.key));
  221.  
  222. storedKeys.forEach(function(item) {
  223. addKeyBox(item.key, item.color, { top: item.top, left: item.left });
  224. });
  225.  
  226. var defaultKeys = [
  227. { key: 'W', color: '#ff0000', top: 50, left: 150 },
  228. { key: 'A', color: '#00ff00', top: 110, left: 90 },
  229. { key: 'S', color: '#0000ff', top: 110, left: 150 },
  230. { key: 'D', color: '#ffff00', top: 110, left: 210 },
  231. { key: 'SPACE', color: '#ff00ff', top: 170, left: 90 },
  232. { key: 'CONTROL', color: '#00ffff', top: 50, left: 60 }
  233. ];
  234.  
  235. defaultKeys.forEach(function(item) {
  236. if (!existingKeys.has(item.key) && !removedKeys.includes(item.key)) {
  237. addKeyBox(item.key, item.color, { top: item.top, left: item.left });
  238. existingKeys.add(item.key);
  239. }
  240. });
  241.  
  242. updateRemoveKeySelect();
  243. }
  244.  
  245. function updateRemoveKeySelect() {
  246. var select = document.getElementById('removeKeySelect');
  247. select.innerHTML = ''; // Clear options
  248. document.querySelectorAll('.key-box').forEach(function(box) {
  249. var option = document.createElement('option');
  250. option.value = box.textContent;
  251. option.textContent = box.textContent;
  252. select.appendChild(option);
  253. });
  254. }
  255.  
  256. function trackRemovedKey(key) {
  257. var removedKeys = JSON.parse(localStorage.getItem('removedKeys')) || [];
  258. if (!removedKeys.includes(key)) {
  259. removedKeys.push(key);
  260. localStorage.setItem('removedKeys', JSON.stringify(removedKeys));
  261. }
  262. }
  263.  
  264. createMenu();
  265. loadKeys();
  266.  
  267. function resetColor(box) {
  268. box.style.backgroundColor = 'transparent';
  269. }
  270.  
  271. function changeColor(box, color) {
  272. box.style.backgroundColor = color;
  273. }
  274.  
  275. document.addEventListener('keydown', function(event) {
  276. var key = event.key.toUpperCase();
  277. if (key === " ") key = "SPACE";
  278. if (key === "CONTROL") key = "CONTROL";
  279.  
  280. var keyBoxes = document.querySelectorAll('.key-box');
  281. keyBoxes.forEach(function(box) {
  282. if (box.textContent === key) {
  283. changeColor(box, box.dataset.color);
  284. }
  285. });
  286. });
  287.  
  288. document.addEventListener('keyup', function(event) {
  289. var key = event.key.toUpperCase();
  290. if (key === " ") key = "SPACE";
  291. if (key === "CONTROL") key = "CONTROL";
  292.  
  293. var keyBoxes = document.querySelectorAll('.key-box');
  294. keyBoxes.forEach(function(box) {
  295. if (box.textContent === key) {
  296. resetColor(box);
  297. }
  298. });
  299. });
  300. setInterval(function() {
  301. var keyBoxes = document.querySelectorAll('.key-box');
  302. keyBoxes.forEach(function(box) {
  303. document.body.appendChild(box);
  304. });
  305. }, 1000);
  306. })();