0
我正在開發某種時間跟蹤器,我需要計算在特定時間段內使用了多少次鼠標或鍵盤。我不是指點擊界面,而是點擊和關鍵事件的共同點。我怎麼攔截它?javafx 8 - 按下的點擊次數和按鍵次數
我正在開發某種時間跟蹤器,我需要計算在特定時間段內使用了多少次鼠標或鍵盤。我不是指點擊界面,而是點擊和關鍵事件的共同點。我怎麼攔截它?javafx 8 - 按下的點擊次數和按鍵次數
我終於找到了一個解決方案:
首先,我們需要創建一個監聽類
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseInputListener;
public class NativeMouseListener implements NativeMouseInputListener {
public void nativeMouseClicked(NativeMouseEvent e) {
System.out.println("Mosue Clicked: " + e.getClickCount());
}
public void nativeMousePressed(NativeMouseEvent e) {
//System.out.println("Mosue Pressed: " + e.getButton());
}
public void nativeMouseReleased(NativeMouseEvent e) {
//System.out.println("Mosue Released: " + e.getButton());
}
public void nativeMouseMoved(NativeMouseEvent e) {
//System.out.println("Mosue Moved: " + e.getX() + ", " + e.getY());
}
public void nativeMouseDragged(NativeMouseEvent e) {
//System.out.println("Mosue Dragged: " + e.getX() + ", " + e.getY());
}
}
然後,做在你的代碼如下:
try {
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
GlobalScreen.addNativeMouseListener(new NativeMouseListener());
您可以從jnative罐子有:
https://github.com/kwhat/jnativehook/releases/
但我不確定它是否適用於Mac。稍後再試。希望有幫助
http://stackoverflow.com/questions/29962395/how-to-write-a-keylistener-for-javafx – Stefan
謝謝,但我不知道它會適用於桌面的任何角落。例如,我最小化了我的應用,它仍然會記錄點擊次數。我猜這不行,但我會嘗試,謝謝 – avalon