-1

Let's imagine I have a program like the source below:

public class Main extends JFrame implements MouseListener {
    public Main() {
        this.setVisible(true);
        this.addMouseListener(this);
    }
    public static void main(String[] args) {
        new Main();
    }
    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("clicked!");
    }
}

in order to run this program and have out output (in this case when user clicked on the jframe shows "clicked") the user has to specifically click the jframe but I want to say if user clicked anywhere show me this output but I don't know how to make the program do that

  • You mean you want to click anywhere on screen (even on the desktop or other applications) and detect the click? – MarsAtomic Jun 22 '20 at 16:53
  • Yes exactly, but it's not just click, at some point I want the program just be underground and just give me information about what the user did when the program wasn't up on his/her screen. in this example I wanna know if the user did click somewhere around the screen or not while the program was running underground – W4rd3nclyffe Jun 22 '20 at 16:55
  • You know that this question has been asked on Stack Overflow many times before? Please get in the habit of searching before posting a question. – MarsAtomic Jun 22 '20 at 17:09
  • I did search for but I couldn't find it, because I didn't know what I was exactly looking for and I searched multiple times for it with different vocabulary but still there was nothing to use. at the end I didn't have too much of a choice, so I just asked in here. this community is for asking QUESTIONS, I don't know why some of you can't just stand some broken questions that come from noobies. I get it, it'll make the site messy but it wouldn't be so hard just to give me the link. Which in this case rwitserloot helped me out. – W4rd3nclyffe Jun 22 '20 at 19:41
  • You got the link -- it was in my second comment identifying the duplicate, and it's now plastered at the top of your question. Stack Overflow is as useful as it is **because** people are strict about what gets asked and answered here. Read the Stack Overflow [help file](https://stackoverflow.com/help). You agreed to all of that when you first posted here, so stop taking it personally -- this isn't facebook. – MarsAtomic Jun 23 '20 at 01:03

1 Answers1

0

You're adding a mouselistener onto your JFrame; of course this only fires if a click happens inside your jframe.

A good OS won't let you do this without registering as an accessibility tool or otherwise getting root access; seeing clicks in other apps is a security leak.

Java is not a great tool for writing OS-specific programs, and 'I want to listen to clicks anywhere on any screen and in any app' is quite OS specific.

Check out this answer for some hacks you can try if you insist on doing this with java.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72