Make Robot hold button down for a certain amount of time without halting the program

Viewed 29

I have this code for a Discord bot so far that when a user sends a message with "up" in it, it presses the W key for 3 seconds, for example. I included up and right for the purpose of an example

@Override
    public void onMessageReceived(MessageReceivedEvent event) {
        Message msg = event.getMessage();

        if (msg.getContentRaw().equals("up")) {
            MessageChannel channel = event.getChannel();
            channel.sendMessage("Going up!").queue();
            robot.keyPress(KeyEvent.VK_W);
            robot.delay(3 * 1000); // In milliseconds
            robot.keyRelease(KeyEvent.VK_W);
        }

        if (msg.getContentRaw().equals("right")) {
            MessageChannel channel = event.getChannel();
            channel.sendMessage("Going right!").queue();
            robot.keyPress(KeyEvent.VK_D);
            robot.delay(3 * 1000); // In milliseconds
            robot.keyRelease(KeyEvent.VK_D);
        }

The problem I have here is that if a user sends "up" and another user sends "right" before the 3 seconds are up, it will "queue" the messages and hold "w" for 3 seconds and then "d" for 3 seconds, but I want it to hold "w" and hold "d" at the same time for each of their respective times. How can I achieve this so it doesn't queue them and actually presses and reads messages at the same time, if possible?

0 Answers
Related