2010-12-04 193 views
4

請幫我在下面的問題註冊計算系統空閒時間

如何找出系統的空閒時間,意味着calcuclate用戶保持系統空閒時(即不移動鼠標,並沒有觸碰鍵盤)以及系統處於空閒狀態的時間。此外,我還應該要求用Excel或郵件發送給用戶,並將該特定系統的當天所有空閒時間總和&發送給用戶。

Regards, Chandu。

+4

我想你應該使用JNI/JNA來做到這一點......對於Windows,你應該調用`GetLastInputInfo`(不知道* X)。我不認爲「搖擺」與此有什麼關係。 – khachik 2010-12-04 09:25:17

回答

5

您可以找出用戶的鼠標使用PointerInfo

MouseInfo.getPointerInfo().getLocation() 

保持輪詢指針位置使用此方法,如果你發現,自從你上次檢查時的位置發生了變化,重置空閒時間回0。下面是一些測試代碼:

public static void main(String[] args) throws Exception { 

    long idleTime = 0 ; 
    long start = System.currentTimeMillis(); 
    Point currLocation = MouseInfo.getPointerInfo().getLocation(); 
    while(true){ 
     Thread.sleep(1000); 
     Point newLocation = MouseInfo.getPointerInfo().getLocation(); 
     if(newLocation.equals(currLocation)){ 
      //not moved 
      idleTime = System.currentTimeMillis() - start; 
     } 
     else{ 
      System.out.printf("Idle time was: %s ms", idleTime); 
      idleTime=0; 
      start = System.currentTimeMillis(); 
      break; 
     } 
     currLocation = newLocation; 

    } 
} 

此外看一看this blog post,使用JNA檢測空閒時間。