2

I'm new in java and trying to make bot for online game and get some practice. And i have some questions, how to make my KeyListener to work when my program (java) minimized so i can minimize my java bot then open my game (its in window mode) and press F2 to start my bot working.

Here is my code:

public class Main extends JFrame implements KeyListener {
    private static Robot robot = null;

    public Main(){
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        setSize(100,100);
        setVisible(true);
        addKeyListener(this);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


    public static void main(String[] args)  {
        // write your code here
        Main  f = new Main();


    }
    public static void click(int x,int y){
        robot.mouseMove(x,y);
        robot.delay(500);
        robot.mousePress(MouseEvent.BUTTON1_MASK);
        robot.delay(500);
        robot.mouseRelease(MouseEvent.BUTTON1_MASK);
        robot.delay(500);

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_F2){
            System.out.println("fdsdfg");
            click(420,290);
            click(300,150);
            //Color grey = new Color(236,236,236);
            Color pixelColor = robot.getPixelColor(420,290);


            if(pixelColor.equals(new Color(238,238,238)))

                System.out.println(pixelColor);
            System.out.println(pixelColor);
        }
        if(e.getKeyCode()==KeyEvent.VK_F3){
            System.exit(0);
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

Sorry for bad english

  • 1
    Does this answer your question? [Listening for input without focus in Java](https://stackoverflow.com/questions/901224/listening-for-input-without-focus-in-java) – Roddy of the Frozen Peas Jul 12 '21 at 14:24

0 Answers0