'How to send Java key inputs to a certain window?

Is there a way to send key inputs to a certain window without putting the window in focus?

For Example, I want to spam the letter A on a google doc while I watch a movie or something.

I am aware that AutoHotKey has a feature like this. Here is my code:

public void run() {
        Robot robot;

        try {
            robot = new Robot();
        } catch (Exception e) {
            return;
        }

        for (double i = 5000; i > 0; i-=100) {
            System.out.println("Starting in " + i/1000 + " seconds");
            wait(100);
        }

        while (GetLocation.getLocation(mainGUI.url).session.mode.equals("dynamic")) {
            // I want to make the following run while I am tabbed out in another window, 
               and I only want it to send the inputs to that window


            robot.keyPress(KeyEvent.VK_SHIFT);
            robot.keyPress(KeyEvent.VK_W);
            robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
        }

        robot.keyRelease(KeyEvent.VK_SHIFT);
        robot.keyRelease(KeyEvent.VK_W);
        robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source