0

I am learning java as a beginner and I was tasked to make an auto-click tool. I used JFrame as an Interface but now I'm stuck with this problem: - I want to get location of pointer related to screen after user clicked a button (Ctrl for example). I added a button on JFrame, click it and the JFrame will be minimized so user can move the pointer around desktop. - Whenever user pressed Ctrl key, I should println the x,y position of the pointer at that time. - I've been swum around to find a solution, but it seemed like my brain couldn't catch up.

It would be a very appreciation if anyone can provide me a simple example. Thanks. My idea so far:

     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     this.setState(JFrame.ICONIFIED);
     this.addKeyListener(this);
     }

     @Override
     public void keyPressed(KeyEvent e) {
     if (e.getKeyCode() == KeyEvent.CTRL_DOWN_MASK) {
                PointerInfo a = MouseInfo.getPointerInfo();
                Point b = a.getLocation();
                int x = (int) b.getX();
                int y = (int) b.getY();
                System.out.println(x + ":" + y);
            }
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Questions for pointers to existing documentation are not appropriate, as a rule. Please show the efforts you have made yourself so far. – Felix Frank Jun 26 '14 at 14:28
  • @FelixFrank added. I run it and nothing happened even when I maximize the frame again and hit Ctrl. – user3776000 Jun 26 '14 at 14:56

1 Answers1

2

I want to get location of pointer related to screen after user clicked a button..

On button click:

  • Hide the frame.
  • Get a screenshot of the entire desktop.
  • Show the screenshot in a JLabel in a JWindow
  • Add a MouseListener to the label.
  • Get the Point from the MouseEvent.

E.G. similar to what is shown in this answer.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433