【问题标题】:Can a .exe file be made to run automatically when the mose isn't moved?可以让.exe文件在mose不移动的情况下自动运行吗?
【发布时间】:2019-03-31 18:54:34
【问题描述】:

我做了一个java应用程序,将当前时间显示为数字时钟,我想让文件在鼠标不动10分钟后自动运行。有人有什么想法吗?

附:我是 StackOverflow 和编码的新手,如果这实际上是一个愚蠢的问题,请原谅我。

【问题讨论】:

  • 当然可以,但是您没有提供足够的信息。什么文件? mouse isn't moved for 10 minutes。在哪里?在您的应用程序或整个操作系统上?或者,如果鼠标没有在您的应用程序所在的计算机系统上移动,您是否正在谈论显示您的时钟?如果是后者,请不要担心,因为您需要将应用程序作为服务运行以检测系统鼠标是否已移动。如果没有,则使时钟可见。当鼠标再次移动时,使您的时钟不可见。你在想什么?
  • 对不起,我没有意识到我在说什么含糊不清,我的意思是指鼠标没有在整个操作系统中移动-o tht my File tuns 而不是屏幕保护程序。但是关于“什么”的问题,我真的不知道还能说什么,只能说一个用 java 编写的 .exe 文件。感谢您的回复。

标签: java


【解决方案1】:

根据您的评论,Java 不会生成 .exe 文件。您需要将您的 jar 文件放入一个特殊的可执行包装器中来完成此操作。 Launch4j 可以为您做到这一点。

您希望以Service 的身份运行您的应用程序。这个SO Thread 可以为这个主题提供一些额外的信息。

在您的应用程序中:

设置您的时钟组件,使其不可见。创建一个TimerTask 来监控系统鼠标指针位置(x,y)。在 TimerTask 的 run() 方法中使用 MouseInfo Class 来跟踪鼠标指针的位置。跟踪鼠标上次移动的时间。如果 10 分钟后没有鼠标移动,则显示您的时钟(使其可见)。如果您愿意,当鼠标再次移动时,时钟将再次不可见。您与此相关的代码可能如下所示:

首先声明并初始化四 (4) 个类成员变量:

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

在您的班级某处复制/粘贴此方法。根据需要进行必要的更改:

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);
}

调用 startMouseMonitoring() 方法,球开始滚动。我相信你会弄清楚其余的。

如果要取消 TimerTask 和鼠标监控,可以调用 TimerTask#cancel() 方法:

mouseMonitorTask.cancel();

【讨论】:

  • 非常感谢您的帮助,这远远超出了我习惯的编程水平,我将尝试将其纳入。再次谢谢你。
猜你喜欢
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多