Java實現按鍵精靈的示例代碼

實現效果

背景

對於日常刷課每十分鐘點擊“繼續學習”的行為,或者說是單機遊戲裡某項重復的行為想使其實現“自動化”。我們可以通過JavaFx裡的Robot類來實現。

難點

  • 窗口穿透
  • 鼠標行為的記錄

搭建程序

需要掌握的知識

窗口測試

@Override
public void start(Stage stage) throws Exception {
    Scene scene = new Scene(new Pane (), 320, 240);
    stage.setTitle("按鍵精靈!");
    stage.setScene(scene);
    stage.show();
}

獲取鼠標位置

Robot robot = new Robot ();
//獲得鼠標位置
Point2D mp = robot.getMousePosition ();
System.out.println (mp);

模擬鼠標單擊

MouseButton.PRIMARY

MouseButton.SECONDARY

滾輪子

MouseButton.MIDDLE

模擬鼠標移動

//鼠標移動
robot.mouseMove (new Point2D (800,800));

鼠標行為監聽

//監聽鼠標單擊
pane.setOnMouseClicked (e->{          
     System.out.println (e.getButton ());
     System.out.println (e.getSceneX ());
     System.out.println (e.getSceneY ());
});
//監聽鼠標鍵入
pane.setOnMousePressed (e->{});
//監聽鼠標釋放
pane.setOnMouseReleased (e->{});
//監聽鼠標在摁著某個鍵時的拖動
pane.setOnMouseDragged (e->{});

完整源碼及詳細解釋

public class QMApp extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        /**
         * 內部類,將每次鼠標的行為以及坐標記錄下來
         */
        class MyMouseEvent{
            //鼠標行為
            MouseButton mb;
            //類型
            EventType et;
            //坐標
            Point2D point2D;
            public MyMouseEvent(MouseButton mb, EventType et, Point2D point2D) {
                this.mb = mb;
                this.et=et;
                this.point2D = point2D;
            }
        }
        //創建面板
        VBox pane = new VBox ();
        //對齊方式
        pane.setAlignment (Pos.TOP_LEFT);
        //robot關鍵對象
        Robot robot = new Robot ();
        //記錄鼠標行為
        LinkedList<MyMouseEvent> list = new LinkedList<> ();
        //開始
        Button beginb = new Button ("開始");
        beginb.setTextFill (Color.RED);
        //結束
        Button endb = new Button ("結束");
        endb.setTextFill (Color.RED);
        pane.getChildren ().addAll (beginb,endb);
        //點擊“開始”按鈕時,鼠標行為儲存進列表
        beginb.setOnMouseClicked (m->{
            pane.setOnMouseDragged (e->{
                list.add (new MyMouseEvent (e.getButton (),e.getEventType (),new Point2D (e.getSceneX (),e.getSceneY ())));
            });
            pane.setOnMousePressed (e->{
                list.add (new MyMouseEvent (e.getButton (),e.getEventType (),new Point2D (e.getSceneX (),e.getSceneY ())));
            });
            pane.setOnMouseReleased (e->{
                list.add (new MyMouseEvent (e.getButton (),e.getEventType (),new Point2D (e.getSceneX (),e.getSceneY ())));
            });
        });
        //點擊“結束”按鈕時,鼠標行為儲存進列表
        endb.setOnMouseClicked (e->{
            pane.getChildren ().remove (beginb);
            for (int i = 0; i < list.size (); i++) {

                //每次鼠標模擬都將窗口透明,實現穿透
                stage.setOpacity (0);
                if (list.get (i).et== MOUSE_DRAGGED){
                    robot.mousePress (list.get (i).mb);
                }else {
                    robot.mouseMove (list.get (i).point2D);
                    robot.mouseClick (list.get (i).mb);
                }
                try {
                    //為瞭效果明顯,進行延遲
                    Thread.sleep (50);
                } catch (InterruptedException ex) {
                    ex.printStackTrace ();
                }
            }
        });
        //面板不參與計算邊界。鼠標點擊事件發生後,會計算應該是哪個組件位於鼠標所在點的位置,而該面板因為不參與邊界計算,所以也不會捕獲到鼠標事件
        pane.setPickOnBounds(false);
        //窗口在前,不能拖到,都沒有
        stage.initStyle (StageStyle.UNDECORATED);
        //窗口最大化
        stage.setMaximized (true);
        //窗口透明度,為瞭使得開始按鈕不會消失同時可以看見其他窗口,半透明
        stage.setOpacity (0.3);
        Scene scene = new Scene(pane);
        stage.setTitle("按鍵精靈");
        stage.setScene(scene);
        stage.show();
    }


    public static void main(String[] args) {
        launch (args);
    }
}

到此這篇關於Java實現按鍵精靈的示例代碼的文章就介紹到這瞭,更多相關Java按鍵精靈內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: