0
// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://hordes.io/play
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

document.addEventListener('keydown', function(evt) {
    if (evt.key === "N") {
        element.dispatchEvent(new KeyboardEvent('keydown', {'key': '2'}));
        element.dispatchEvent(new KeyboardEvent('keydown', {'key': '3'}));
        element.dispatchEvent(new KeyboardEvent('keydown', {'key': '5'}));
        element.dispatchEvent(new KeyboardEvent('keydown', {'key': '6'}));
        }
    });

I want to make the code stimulate pressing keys on the keyboard (2, 3, 5, and 6) when I press the key "N". This is a Tampermonkey script, if that information is helpful. I'm having trouble finding what I should define the variable "element" as. Thank you! :)

Also, does anyone know a website that tells me all the keycodes for the keyboard keys?

  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 08 '22 at 19:30
  • This website has some keyboard codes. http://gcctech.org/csc/javascript/javascript_keycodes.htm –  Oct 08 '22 at 23:58
  • Unfortnaly, it impossible to "simulate" a keypress in JS, but you can try to add the function you want to trigger when pressing 2,3,5,6, to a on-keydown event. –  Oct 09 '22 at 00:02
  • https://stackoverflow.com/questions/4135100/i-have-a-dropdown-menu-how-can-i-trigger-js-function-on-keydown –  Oct 09 '22 at 00:03
  • @Wervice https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically – Konrad Oct 09 '22 at 07:19
  • 1
    Ok, i see, it's possible. I'm sorry. –  Oct 09 '22 at 09:58

0 Answers0