How to Obtain Mouse Click Coordinates Outside My Window in Java

how to obtain mouse click coordinates outside my window in Java

It is possible though limited:

Add an AWTEventListener for focus events. As long as your app has focus before the button is clicked you'll receive a focus lost event. Then query for the pointer position.

The limitation is that, of course, your app loses focus. So depending on what you are ultimately trying to achieve this might not be useful.

If you don't want to lose focus then you will have to temporarily take a screenshot of the whole screen and display that in a screen filling window which listens for a mouse click as usual.

Proof of first method:

import java.awt.AWTEvent;
import java.awt.MouseInfo;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;

import javax.swing.JFrame;

public class Application1 {
public static void main(String[] args) {
Toolkit.getDefaultToolkit().addAWTEventListener(
new Listener(), AWTEvent.MOUSE_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private static class Listener implements AWTEventListener {
public void eventDispatched(AWTEvent event) {
System.out.print(MouseInfo.getPointerInfo().getLocation() + " | ");
System.out.println(event);
}
}
}

Clicking outside of the app produced:

java.awt.Point[x=198,y=59] | java.awt.event.MouseEvent[MOUSE_EXITED, ...
java.awt.Point[x=976,y=503] | java.awt.FocusEvent[FOCUS_LOST, ...

The second point is outside of the app.

How to get location of a mouse click relative to a swing window

From MouseListener methods you can do:

@Override
public void mouseClicked(MouseEvent e) {
int x=e.getX();
int y=e.getY();
System.out.println(x+","+y);//these co-ords are relative to the component
}

Simply add this to your Component by:

component.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
});

Reference:

  • How to Write a Mouse Listener

Find position of mouse outside of JFrame?

MouseInfo.getPointerInfo().getLocation().x;
MouseInfo.getPointerInfo().getLocation().y;

How do I get the mouse position on click when it is outside of the component?

I decided to just make a translucent window the size of the screen (which is barely visible) and collected the mouse positions that way. It seemed to work, but I was hoping that I wouldn't have to paint an extra component.

Summarized (for those that care):

  • Make translucent window
  • User is able to see through the window and can click where one wants
  • Mouse coordinates are collected
  • Window is discarded

So far this seems to be my best option, unless someone else mentions one shortly :)

How can I get the mouse coordinates relative to a JFrame rather than the entire screen

I want to get the coordinates of my mouse... I am using a screen with an array of JPanels that change colour when I click on them.

You don't need the mouse coordinates. You can just use the getComponent() method of the MouseEvent to get the component you clicked on. Then change the background.

arrGrid[y][x].addMouseListener(new MouseAdapter() {

Your current code is creating a unique listener for every component. This is unnecessary.

Instead create a generic listener that can be shared by all panels. So outside your looping code you would do something like:

MouseListener ml = new MouseAdapter()
{
@Override mousePressed(MouseEvent e)
{
Component panel = e.getComponent();
panel.setBackground( Color.GRAY );
}
};

Then in your looping code you simply do:

arrGrid[y][x].addMouseListener( ml );

Detecting a Mouse Click Anywhere on Screen with Java

You can do this only with platform specific implementation of the OS API, as you can't detect clicks outside from your program in your program itself.

While you won't get around writing platform specific code, just abstract it as an interface and use different implementations appropiately.



Related Topics



Leave a reply



Submit