Java實(shí)現(xiàn)按鍵精靈的示例代碼
實(shí)現(xiàn)效果

背景
對(duì)于日常刷課每十分鐘點(diǎn)擊“繼續(xù)學(xué)習(xí)”的行為,或者說(shuō)是單機(jī)游戲里某項(xiàng)重復(fù)的行為想使其實(shí)現(xiàn)“自動(dòng)化”。我們可以通過(guò)JavaFx里的Robot類(lèi)來(lái)實(shí)現(xiàn)。
難點(diǎn)
- 窗口穿透
- 鼠標(biāo)行為的記錄
搭建程序

需要掌握的知識(shí)
窗口測(cè)試
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new Pane (), 320, 240);
stage.setTitle("按鍵精靈!");
stage.setScene(scene);
stage.show();
}獲取鼠標(biāo)位置
Robot robot = new Robot (); //獲得鼠標(biāo)位置 Point2D mp = robot.getMousePosition (); System.out.println (mp);

模擬鼠標(biāo)單擊
左
MouseButton.PRIMARY
右
MouseButton.SECONDARY
滾輪子
MouseButton.MIDDLE
模擬鼠標(biāo)移動(dòng)
//鼠標(biāo)移動(dòng) robot.mouseMove (new Point2D (800,800));
鼠標(biāo)行為監(jiān)聽(tīng)
//監(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->{});完整源碼及詳細(xì)解釋
public class QMApp extends Application {
@Override
public void start(Stage stage) throws Exception {
/**
* 內(nèi)部類(lèi),將每次鼠標(biāo)的行為以及坐標(biāo)記錄下來(lái)
*/
class MyMouseEvent{
//鼠標(biāo)行為
MouseButton mb;
//類(lèi)型
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);
}
}到此這篇關(guān)于Java實(shí)現(xiàn)按鍵精靈的示例代碼的文章就介紹到這了,更多相關(guān)Java按鍵精靈內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
hibernate關(guān)于session的關(guān)閉實(shí)例解析
這篇文章主要介紹了hibernate關(guān)于session的關(guān)閉實(shí)例解析,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
簡(jiǎn)單談?wù)凾hreadPoolExecutor線程池之submit方法
下面小編就為大家?guī)?lái)一篇簡(jiǎn)單談?wù)凾hreadPoolExecutor線程池之submit方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
SpringBoot通過(guò)@MatrixVariable進(jìn)行傳參詳解
這篇文章主要介紹了SpringBoot使用@MatrixVariable傳參,文章圍繞@MatrixVariable展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
Java多線程之readwritelock讀寫(xiě)分離的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java多線程之readwritelock讀寫(xiě)分離的相關(guān)內(nèi)容,文中涉及具體實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
Java中數(shù)組與集合的相互轉(zhuǎn)換實(shí)現(xiàn)解析
這篇文章主要介紹了Java中數(shù)組與集合的相互轉(zhuǎn)換實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Java中構(gòu)造函數(shù),set/get方法和toString方法使用及注意說(shuō)明
這篇文章主要介紹了Java中構(gòu)造函數(shù),set/get方法和toString方法的使用及注意說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
RestTemplate請(qǐng)求失敗自動(dòng)重啟機(jī)制精講
這篇文章主要為大家介紹了RestTemplate請(qǐng)求失敗自定義處理的方法,自動(dòng)重試的機(jī)制精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多所進(jìn)步,早日升職加薪2022-03-03
例舉fastJson和jackson轉(zhuǎn)json的區(qū)別
今天小編就為大家分享一篇關(guān)于例舉fastJson和jackson轉(zhuǎn)json的區(qū)別,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12

