-2

i've made a java aplication that displays the current time as a digital clock, and i would like to make the file automatically run after the mouse isn't moved for 10 minutes.Does anyone have any ideas?

P.S. I'm new to StackOverflow and to coding as well at that, so forgive me if this is actualy a stupid question.

  • Absolutely but, you haven't supplied near enough information. What file? `mouse isn't moved for 10 minutes`. Where? Over your application or across the entire Operating System? Or are you talking about displaying your clock if the mouse has not moved on the computer system your application is stored in? If it's the latter then don't worry about it since you would need to run your application as a Service in order to detect whether or not the System Mouse has moved or not. If not then make the clock visible. When the mouse moves again make your clock non-visible. What are you thinking about? – DevilsHnd Apr 01 '19 at 05:01
  • I’m sorry, I didn’t realise how vague I was beeing, i meant to refer to the mouse not beeing moved across the entire OS S-o tht my File tuns instead of a screen saver. But regarding the “what” question i realy do not know what else to Say but a .exe File written in java. Thankyou for responding. – Silviu Grada Apr 01 '19 at 06:23

1 Answers1

0

As per your comment, Java doesn't make .exe files. You would need to place your jar file into a special executable wrapper to accomplish that. Launch4j can do that for you.

You would want to run your application as a Service. This SO Thread can shed some additional light on that subject.

In your application:

Set your clock component so that it is non-visible. Create a TimerTask to monitor the System Mouse pointer location (x, y). Utilize the MouseInfo Class within the TimerTask's run() method to track the Mouse Pointer location. Keep track of the time from the mouse last movement. If 10 minutes has elapsed with no mouse movement then display your clock (make it visible). If you like, when the mouse is moved again make the clock non-visible again. Your code in relation to this might look something like this:

First declare and initialize four (4) Class Member Variables:

int mouseX = 0;
int mouseY = 0;
long timeOfLastMovement = 0L;
TimerTask mouseMonitorTask;

Somewhere in your Class copy/paste this method. Make the required changes as you see fit:

private void startMouseMonitoring() {
    mouseMonitorTask = new TimerTask() {
        @Override
        public void run() {
            PointerInfo info = MouseInfo.getPointerInfo();
            Point pointerLocation = info.getLocation();
            long currentTime = java.lang.System.currentTimeMillis();
            //System.out.format("Mouse Location - X: %d, Y: %d\n", pointerLocation.x, pointerLocation.y);
            float elapsedTime = (((currentTime - timeOfLastMovement) / 1000F) / 60);
            if (pointerLocation.x == mouseX && pointerLocation.y == mouseY) {
                // Check if 10 minutes has elapsed with no mouse movement
                if (elapsedTime >= 10.0f) {
                    /* Make Clock Visible if it isn't already 
                       or whatever else you want to do.  */
                    if (clockIsNonVisible) {
                        // clock.setVisible(true);
                    }
                }
            }
            else {
                mouseX = pointerLocation.x;
                mouseY = pointerLocation.y;
                timeOfLastMovement = currentTime;
                // Make clock non-visible if you like.
                if (clockIsVisible) {
                    // clock.setVisible(false);  
                }
            }

            try {
                Thread.sleep(500);
            }
            catch (InterruptedException e) {
                cancel();
                e.printStackTrace();
            }
        }
    };

    Timer monitorTimer = new Timer("Timer");

    long delay = 1000L;  // Start Delay: 1 second
    long period = 1000L; // Cycle every: 1 second
    monitorTimer.scheduleAtFixedRate(mouseMonitorTask, delay, period);
}

Call the startMouseMonitoring() method and the ball is rolling. I'm sure you'll figure out the rest.

If you want to cancel the TimerTask and Mouse Monitoring then you can call the TimerTask#cancel() method:

mouseMonitorTask.cancel();
DevilsHnd
  • 6,334
  • 2
  • 14
  • 19
  • Thankyou very much for your help, this is waaaay above the level of programming i am used to, i will try to incorporte this. And again thankyou. – Silviu Grada Apr 01 '19 at 12:05