Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

This userscript is not pressing the keycodes when I press the numpad keys, can you fix…

  1. User

    This userscript is not pressing the keycodes when I press the numpad keys, can you fix it?

    // ==UserScript== // @name lol2 // @namespace http://tampermonkey.net/ // @match http*://arras.io/* // @version 1.0 // @description lol // @grant none // ==/UserScript== (function() { 'use strict';

    const targetkeyCodes = [78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,72,85,74,49,49,49,49,50,50,50,50,50,51,51,52,52,52,52,52,52,53,53,53,53,53,53,53,54,54,54,54,54,54,55,55,55,55,55,55,55,56,56,56,56,56]; const trapper = [78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,49,49,50,50,50,51,51,52,52,52,52,52,52,53,53,53,53,53,54,54,54,54,54,54,54,55,55,55,55,55,55,55,55,55,56,56,56,56,57,48,48,48]; const conq = [78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,49,49,49,49,50,50,50,50,51,51,51,51,51,52,52,52,52,52,53,53,53,54,54,54,54,54,54,54,55,55,55,55,55,55,55,55,55,56,56,56,56,56]; const anni = [78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,49,49,49,49,49,50,50,50,50,50,51,51,51,51,51,51,52,52,52,52,53,53,53,53,54,54,54,54,55,55,55,56,56,56,56,56,56,57,48,48,48,48];

    let index = 0;

    window.keydown = function() {};
    window.addEventListener = new Proxy(window.addEventListener, {apply: function(a, b, c) {
        if(c[0] === 'keydown') {
            window.keydown = c[1]
        }
        return Reflect.apply(a, b, c)
    } });
    

    window.addEventListener('keydown', function(event) { if (event.key === 'Numpad1') { index = 0; triggerKeyDown(); } else if (event.key === 'Numpad2') { index = 0; triggerKeyDown2(); } else if (event.key === 'Numpad3') { index = 0; triggerKeyDown3(); } else if (event.key === 'Numpad4') { index = 0; triggerKeyDown4(); } });

    function triggerKeyDown() { if (index < targetkeyCodes.length) { const keyCode = targetkeyCodes[index]; window.keydown({ isTrusted: true, keyCode}); index++; triggerKeyDown(); } } function triggerKeyDown2() { if (index < trapper.length) { const keyCode = trapper[index]; window.keydown({ isTrusted: true, keyCode}); index++; triggerKeyDown2(); } } function triggerKeyDown3() { if (index < conq.length) { const keyCode = conq[index]; window.keydown({ isTrusted: true, keyCode}); index++; triggerKeyDown3(); } } function triggerKeyDown4() { if (index < anni.length) { const keyCode = anni[index]; window.keydown({ isTrusted: true, keyCode}); index++; triggerKeyDown4(); } }

    })();

  2. Assistant

    The script you provided seems to mostly be set up correctly for simulating keypress events. However, there are a couple of things to check and potentially correct:

    1. Ensure that the script is actually running on the target page by looking for console logs or other indicators that it's been injected and executed properly.
    2. Verify that the event.key values you're checking match the values the browser produces. Different browsers or operating systems may have different values for specific keys. You could use event.code instead, which is more consistent.

    Here's a revised version of your script with some added logging and using event.code. In addition, I adjusted the recursive calls to be non-blocking by using setTimeout with a delay of 0, this stops the function from locking the browser with a synchronous recursive loop:

    // ==UserScript==
    // @name        lol2
    // @namespace   http://tampermonkey.net/
    // @match       http*://arras.io/*
    // @version     1.0
    // @description lol
    // @grant       none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        // ...[arrays code omitted for brevity]...
    
        let index = 0;
    
        window.keydown = function() {};
        window.addEventListener = new Proxy(window.addEventListener, {
            apply: function(a, b, c) {
                if (c[0] === 'keydown') {
                    window.keydown = c[1];
                }
                return Reflect.apply(a, b, c);
            }
        });
    
        window.addEventListener('keydown', function(event) {
            if (event.code === 'Numpad1') {
                index = 0;
                triggerKeyDown(targetkeyCodes);
            } else if (event.code === 'Numpad2') {
                index = 0;
                triggerKeyDown(trapper);
            } else if (event.code === 'Numpad3') {
                index = 0;
                triggerKeyDown(conq);
            } else if (event.code === 'Numpad4') {
                index = 0;
                triggerKeyDown(anni);
            }
        });
    
        function triggerKeyDown(keyArray) {
            if (index < keyArray.length) {
                const keyCode = keyArray[index];
                console.log('Simulating keydown:', keyCode); // Log simulated key press
                window.keydown({ isTrusted: true, keyCode: keyCode });
                index++;
                setTimeout(() => triggerKeyDown(keyArray), 0); // Queue the next keydown event
            }
        }
    
    })();
    

    Make sure to confirm the following points:

    • Arrays (targetkeyCodes, trapper, conq, anni) contain correct key codes and they match the key mappings of the operating system and browser you're using.
    • The target application you're trying to simulate the key presses in handles synthetic events (created in JavaScript; isTrusted: true you added might be ignored by some event handlers or may not be allowed to change).
    • The event handler that you're hooking into (window.keydown) is the correct handler used by the game/application. Sometimes applications have more complex or specific ways of handling key events.