您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Java如何實(shí)現(xiàn)按鍵精靈”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Java如何實(shí)現(xiàn)按鍵精靈”吧!
對(duì)于日常刷課每十分鐘點(diǎn)擊“繼續(xù)學(xué)習(xí)”的行為,或者說(shuō)是單機(jī)游戲里某項(xiàng)重復(fù)的行為想使其實(shí)現(xiàn)“自動(dòng)化”。我們可以通過(guò)JavaFx里的Robot類來(lái)實(shí)現(xiàn)。
窗口穿透
鼠標(biāo)行為的記錄
@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 (); //獲得鼠標(biāo)位置 Point2D mp = robot.getMousePosition (); System.out.println (mp);
左
MouseButton.PRIMARY
右
MouseButton.SECONDARY
滾輪子
MouseButton.MIDDLE
//鼠標(biāo)移動(dòng) robot.mouseMove (new Point2D (800,800));
//監(jiān)聽(tīng)鼠標(biāo)單擊 pane.setOnMouseClicked (e->{ System.out.println (e.getButton ()); System.out.println (e.getSceneX ()); System.out.println (e.getSceneY ()); }); //監(jiān)聽(tīng)鼠標(biāo)鍵入 pane.setOnMousePressed (e->{}); //監(jiān)聽(tīng)鼠標(biāo)釋放 pane.setOnMouseReleased (e->{}); //監(jiān)聽(tīng)鼠標(biāo)在摁著某個(gè)鍵時(shí)的拖動(dòng) pane.setOnMouseDragged (e->{});
public class QMApp extends Application { @Override public void start(Stage stage) throws Exception { /** * 內(nèi)部類,將每次鼠標(biāo)的行為以及坐標(biāo)記錄下來(lái) */ class MyMouseEvent{ //鼠標(biāo)行為 MouseButton mb; //類型 EventType et; //坐標(biāo) Point2D point2D; public MyMouseEvent(MouseButton mb, EventType et, Point2D point2D) { this.mb = mb; this.et=et; this.point2D = point2D; } } //創(chuàng)建面板 VBox pane = new VBox (); //對(duì)齊方式 pane.setAlignment (Pos.TOP_LEFT); //robot關(guān)鍵對(duì)象 Robot robot = new Robot (); //記錄鼠標(biāo)行為 LinkedList<MyMouseEvent> list = new LinkedList<> (); //開(kāi)始 Button beginb = new Button ("開(kāi)始"); beginb.setTextFill (Color.RED); //結(jié)束 Button endb = new Button ("結(jié)束"); endb.setTextFill (Color.RED); pane.getChildren ().addAll (beginb,endb); //點(diǎn)擊“開(kāi)始”按鈕時(shí),鼠標(biāo)行為儲(chǔ)存進(jìn)列表 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 ()))); }); }); //點(diǎn)擊“結(jié)束”按鈕時(shí),鼠標(biāo)行為儲(chǔ)存進(jìn)列表 endb.setOnMouseClicked (e->{ pane.getChildren ().remove (beginb); for (int i = 0; i < list.size (); i++) { //每次鼠標(biāo)模擬都將窗口透明,實(shí)現(xiàn)穿透 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 { //為了效果明顯,進(jìn)行延遲 Thread.sleep (50); } catch (InterruptedException ex) { ex.printStackTrace (); } } }); //面板不參與計(jì)算邊界。鼠標(biāo)點(diǎn)擊事件發(fā)生后,會(huì)計(jì)算應(yīng)該是哪個(gè)組件位于鼠標(biāo)所在點(diǎn)的位置,而該面板因?yàn)椴粎⑴c邊界計(jì)算,所以也不會(huì)捕獲到鼠標(biāo)事件 pane.setPickOnBounds(false); //窗口在前,不能拖到,都沒(méi)有 stage.initStyle (StageStyle.UNDECORATED); //窗口最大化 stage.setMaximized (true); //窗口透明度,為了使得開(kāi)始按鈕不會(huì)消失同時(shí)可以看見(jiàn)其他窗口,半透明 stage.setOpacity (0.3); Scene scene = new Scene(pane); stage.setTitle("按鍵精靈"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch (args); } }
到此,相信大家對(duì)“Java如何實(shí)現(xiàn)按鍵精靈”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。