0

Currently stuck on how to create an executable jar file that would run in the background of my pc and detect if my mouse is down. I know JFrame is a one method of doing so, but that's visible on my screen, even though I set it to invisible it appears to disable it completely.

Here's my code so far, is there a another method I could use that isn't JFrame related?

public class MyFrame extends JFrame implements KeyListener {

MyFrame(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(500,500);
    this.setLayout(null);
    this.addKeyListener(this);
    this.setVisible(true);
    this.setAlwaysOnTop(true);

}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {
    if(e.getKeyChar() =='q'){
        this.setVisible(false);
    }
    if(e.getKeyChar()=='l'){
        this.setVisible(true);
    }
}

}

djBroski
  • 3
  • 1
  • Does this answer your question? [java - Detect Mouse Clicks Anywhere On Screen](https://stackoverflow.com/questions/25496292/java-detect-mouse-clicks-anywhere-on-screen) – tgdavies Jun 12 '22 at 05:34
  • 2
    You can’t do it without using some kind of native library - JNI/JNA – MadProgrammer Jun 12 '22 at 06:09
  • [Example to play with using JNativeHook](https://github.com/kwhat/jnativehook/blob/2.2/doc/Mouse.md). You can get the library JAR file [here](https://jar-download.com/artifacts/com.1stleg/jnativehook/2.1.0/source-code). – DevilsHnd - 退職した Jun 12 '22 at 09:27

1 Answers1

0

You can download jNativeHook and hook the global listener to globalScreen.

you can use it as a normal swing listener.

Here is the link:

https://code.google.com/p/jnativehook/

By using this library you achieve a wide range of functions to control the mouse events!!

Sobhan
  • 1,280
  • 1
  • 18
  • 31