2010-12-04 243 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检测空闲时间。