Tuesday 15 March 2011

windows - Using java how to find out an application is not used for sometime? -



windows - Using java how to find out an application is not used for sometime? -

using java how find out application not used sometime?

i having 3rd party application illustration can take 'skype'. if there no action (mouse/keyboard input)is given skype 5min, code through popup user. how check whether application getting input user? gone through net found below code giving output if entire desktop idle. need specific application skype. how that?

class="snippet-code-html lang-html prettyprint-override">import java.text.dateformat; import java.text.simpledateformat; import java.util.date; import com.sun.jna.*; import com.sun.jna.win32.*; /** * utility method retrieve idle time on windows , sample code test it. * jna shall nowadays in classpath work (and compile). * @author ochafik */ public class win32idletime { public interface kernel32 extends stdcalllibrary { kernel32 instance = (kernel32)native.loadlibrary("kernel32", kernel32.class); /** * retrieves number of milliseconds have elapsed since scheme started. * @see http://msdn2.microsoft.com/en-us/library/ms724408.aspx * @return number of milliseconds have elapsed since scheme started. */ public int gettickcount(); }; public interface user32 extends stdcalllibrary { user32 instance = (user32)native.loadlibrary("user32", user32.class); /** * contains time of lastly input. * @see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputstructures/lastinputinfo.asp */ public static class lastinputinfo extends construction { public int cbsize = 8; /// tick count of when lastly input event received. public int dwtime; } /** * retrieves time of lastly input event. * @see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getlastinputinfo.asp * @return time of lastly input event, in milliseconds */ public boolean getlastinputinfo(lastinputinfo result); }; /** * amount of milliseconds have elapsed since lastly input event * (mouse or keyboard) * @return idle time in milliseconds */ public static int getidletimemilliswin32() { user32.lastinputinfo lastinputinfo = new user32.lastinputinfo(); user32.instance.getlastinputinfo(lastinputinfo); homecoming kernel32.instance.gettickcount() - lastinputinfo.dwtime; } enum state { unknown, online, idle, away }; public static void main(string[] args) { if (!system.getproperty("os.name").contains("windows")) { system.err.println("error: implemented on windows"); system.exit(1); } state state = state.unknown; dateformat dateformat = new simpledateformat("eee, d mmm yyyy hh:mm:ss"); (;;) { int idlesec = getidletimemilliswin32() / 1000; state newstate = idlesec < 30 ? state.online : idlesec > 5 * 60 ? state.away : state.idle; if (newstate != state) { state = newstate; system.out.println(dateformat.format(new date()) + " # " + state); } seek { thread.sleep(1000); } grab (exception ex) {} } } }

i think not used mean, user don't utilize active. can utilize windowlistener. here illustration working code(hope that's wanted):

public class awaytimer { private jframe mainframe; // mainframe in private jlabel timelabel; // our label stores time when leaving window private long leavetime = 0; // time set zero, prevent later saying unusual numbers public static void main(string[] args) { new awaytimer(); // create new awaytimer object } public awaytimer() { mainframe = new jframe("away timer"); // create new frame name away timer timelabel = new jlabel ("you 0 seconds away.", swingconstants.center); // create new label shows time away windowlistener listener = new windowlistener() { @override public void windowdeactivated(windowevent e) { // called on leaving focus leavetime = system.currenttimemillis(); // time when leaving window , save leavetime } @override public void windowactivated(windowevent e) { // called on switching frame // gets activated when open program. shows <ahugenumber> seconds away. set 0, check if leavetime 0 initialized if (leavetime == 0) return; // dont need calculate , leave text long difference = (system.currenttimemillis() - leavetime) / 1000; // calculate difference between leave time , , split 1000 time in seconds timelabel.settext("you " + difference + " seconds away."); // alter displayed text } // other listeners, arent of import @override public void windowopened(windowevent e) {/* here*/} @override public void windowiconified(windowevent e) {/* here*/} @override public void windowdeiconified(windowevent e) {/* here*/} @override public void windowclosing(windowevent e) {/* here*/} @override public void windowclosed(windowevent e) {/* here*/} }; mainframe.addwindowlistener(listener); // add together created listener window // add together label frame mainframe.add(timelabel); mainframe.pack(); // resize frame, fits contents mainframe.setvisible(true); // create visible } }

hope help.

java windows desktop

No comments:

Post a Comment