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

背景
對于日常刷課每十分鐘點(diǎn)擊“繼續(xù)學(xué)習(xí)”的行為,或者說是單機(jī)游戲里某項(xiàng)重復(fù)的行為想使其實(shí)現(xiàn)“自動(dòng)化”。我們可以通過JavaFx里的Robot類來實(shí)現(xiàn)。
難點(diǎ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();
}獲取鼠標(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)聽
//監(jiān)聽鼠標(biāo)單擊
pane.setOnMouseClicked (e->{
System.out.println (e.getButton ());
System.out.println (e.getSceneX ());
System.out.println (e.getSceneY ());
});
//監(jiān)聽鼠標(biāo)鍵入
pane.setOnMousePressed (e->{});
//監(jiān)聽鼠標(biāo)釋放
pane.setOnMouseReleased (e->{});
//監(jiān)聽鼠標(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)部類,將每次鼠標(biāo)的行為以及坐標(biāo)記錄下來
*/
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 ();
//對齊方式
pane.setAlignment (Pos.TOP_LEFT);
//robot關(guān)鍵對象
Robot robot = new Robot ();
//記錄鼠標(biāo)行為
LinkedList<MyMouseEvent> list = new LinkedList<> ();
//開始
Button beginb = new Button ("開始");
beginb.setTextFill (Color.RED);
//結(jié)束
Button endb = new Button ("結(jié)束");
endb.setTextFill (Color.RED);
pane.getChildren ().addAll (beginb,endb);
//點(diǎn)擊“開始”按鈕時(shí),鼠標(biāo)行為儲存進(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)行為儲存進(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);
//窗口在前,不能拖到,都沒有
stage.initStyle (StageStyle.UNDECORATED);
//窗口最大化
stage.setMaximized (true);
//窗口透明度,為了使得開始按鈕不會(huì)消失同時(shí)可以看見其他窗口,半透明
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven項(xiàng)目打包成war包部署到Tomcat的方法
這篇文章主要介紹了Maven項(xiàng)目打包成war包部署到Tomcat的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
SpringCloud微服務(wù)續(xù)約實(shí)現(xiàn)源碼分析詳解
這篇文章主要介紹了SpringCloud微服務(wù)續(xù)約實(shí)現(xiàn)源碼分析,服務(wù)續(xù)期和服務(wù)注冊非常相似,服務(wù)注冊在Eureka?Client程序啟動(dòng)之后開啟,并同時(shí)開啟服務(wù)續(xù)期的定時(shí)任務(wù)2022-11-11
Spring Boot項(xiàng)目實(shí)戰(zhàn)之?dāng)r截器與過濾器
這篇文章主要介紹了Spring Boot項(xiàng)目實(shí)戰(zhàn)之?dāng)r截器與過濾器,文中給大家詳細(xì)介紹了springboot 攔截器和過濾器的基本概念,過濾器的配置,需要的朋友可以參考下2018-01-01
javaweb項(xiàng)目如何實(shí)現(xiàn)手機(jī)短信登錄
這篇文章主要介紹了javaweb項(xiàng)目如何實(shí)現(xiàn)手機(jī)短信登錄,手機(jī)號登錄在現(xiàn)在的項(xiàng)目中用的場景非常多,實(shí)現(xiàn)起來也不難,今天我們就一起來通過演示實(shí)現(xiàn)登錄過程,需要的朋友可以參考下2019-07-07
Java 實(shí)戰(zhàn)項(xiàng)目之家居購物商城系統(tǒng)詳解流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實(shí)現(xiàn)一個(gè)家居購物商城系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11
Java數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)
線性表(linear?list)是n個(gè)具有相同特性的數(shù)據(jù)元素的有限序列。?線性表是一種在實(shí)際中廣泛使用的數(shù)據(jù)結(jié)構(gòu),本文將用Java實(shí)現(xiàn)順序表,感興趣的可以了解一下2022-09-09
解決java連接虛擬機(jī)Hbase無反應(yīng)的問題
這篇文章主要介紹了解決java連接虛擬機(jī)Hbase無反應(yīng)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
java 中@Deprecated 注解的實(shí)例詳解
這篇文章主要介紹了java 中@Deprecated 注解的實(shí)例詳解的相關(guān)資料,這里對@Deprecated注解進(jìn)行了詳細(xì)介紹,希望能幫助到大家,需要的朋友可以參考下2017-08-08
SpringBoot3讀取配置文件application.properties屬性值的幾種方式
這篇文章主要介紹了SpringBoot3讀取配置文件application.properties屬性值的幾種方式,文中通過代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-11-11

