1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Keypress function

Discussion in 'JavaScript' started by btboff, Dec 20, 2022.

  1. #1
    Hello, my goal is to simulate keyboard presses in JavaScript and send them at regular intervals in the text input of messaging application.
    However, not being a professional of JavaScript, I face several problems in my program below :

    1) Being sure that the keystroke simulation is triggered.
    2) Apply the right input format for the application, in my program I have "windows" but it triggers an error in the execution of the program because it does not run in an HTML page.
    3) Finally, send the function of the simulated keys after "send".

    Here is the end of my program in question :

    var keys = {};

    window.addEventListener('keydown', (e) => {

    keys[e.key] = true;

    if(keys['a'] && keys['v'])
    console.log('A + V');

    });

    window.addEventListener('keyup', (e) => {

    keys[e.key] = false;

    });

    document.dispatchEvent(
    new KeyboardEvent("keydown", {
    key: "v",
    bubbles: true
    })
    );

    document.dispatchEvent(
    new KeyboardEvent("keydown", {
    key: "a",
    bubbles: true
    })
    );

    setInterval(() => {
    client.channels.cache.get('identifier_channel').send();
    }, 50);
    });

    If you have some solutions, thanks in advance for your help !
     
    btboff, Dec 20, 2022 IP
  2. James Cavalcanti

    James Cavalcanti Peon

    Messages:
    14
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    3
    #2
    I can certainly help you with your issues! Here are some suggestions:

    1. To be sure that the keystroke simulation is triggered, you can try calling the dispatchEvent function on the document object, as you are doing, but make sure to use the keydown event type and include the key and bubbles properties in the KeyboardEvent constructor. You can also try using the keyCode property instead of the key property, as some browsers may not support the key property.

    2. To apply the right input format for the application, you will need to know what the expected input format is. You can check the documentation for the messaging application to see what input it expects. If it expects text input, you can simply pass a string as the message to be sent. If it expects a different type of input, you will need to modify your program accordingly.

    3. To send the function of the simulated keys after "send", you will need to modify the send() function to include the simulated keystrokes. You can do this by calling the dispatchEvent function on the document object with the appropriate KeyboardEvent constructor, as I mentioned earlier, before calling the send() function.
    I hope these suggestions are helpful!
     
    James Cavalcanti, Dec 20, 2022 IP
    Vooler likes this.