簡體   English   中英

Java機械手類-模擬鼠標單擊而無需移動光標

[英]Java robot class - simulating mouse clicks without moving the cursor

我正在嘗試模擬真實的鼠標單擊,而不用javas機器人類移動光標。 是否可以將此代碼放入while循環或其他內容中以注冊鼠標位置,並在實際單擊后將鼠標移至該位置? 到目前為止,代碼被告知將鼠標移動到已注冊的鼠標位置(一旦我運行代碼,它就會注冊),但是我希望它將鼠標移動到與我的鼠標所在的位置相同的位置,而不是角落。 謝謝。

           while(true) {
            PointerInfo a = MouseInfo.getPointerInfo();
            Point b  = a.getLocation();
            int xOrig = (int)b.getX();
            int yOrig = (int)b.getY();


        try {
            Robot r = new Robot();
            Thread.sleep(3000);
            r.mouseMove(720, 360);
            r.mousePress(InputEvent.BUTTON1_MASK); //press the left mouse button
            r.mouseRelease(InputEvent.BUTTON1_MASK); //release the left mouse button

            //move the mouse back to the original position
            r.mouseMove(xOrig, yOrig);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

}

只需將Thread.sleep(3000)放在try {}塊的末尾。

您當前的代碼將鼠標移動到舊的原始位置后立即獲取新的“原始位置”

沒有循環

public static void main(String[] args) throws InterruptedException {
    //For testing
    Thread.sleep(1000);

    PointerInfo a = MouseInfo.getPointerInfo();
    Point b = a.getLocation();
    int xOrig = (int) b.getX();
    int yOrig = (int) b.getY();

    try {
        Robot r = new Robot();

        r.mouseMove(720, 360);
        // press the left mouse button
        r.mousePress(InputEvent.BUTTON1_MASK);
        // release the left mouse button
        r.mouseRelease(InputEvent.BUTTON1_MASK);

        // move the mouse back to the original position
        r.mouseMove(xOrig, yOrig);

        Thread.sleep(3000);
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM