I am capturing mouse events inside of a shell with Composites by adding a Filter.
For now the events are positioned relative to the Composites, where they occure.
How can I retrieve MouseEvents absolute position?
(Absolute position is the position with 0,0 in the left uper corner of the display).
parentShell.getDisplay().addFilter(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event event) {
//do not know the absolute position here
}
});
You can use Control#toDisplay(int, int) to convert any relative coordinates to display relative absolute values.
public void handleEvent(Event event) {
if (event.widget instanceof Control) {
Point absolutePos = ((Control) event.widget).toDisplay(event.x, event.y);
...
}
}
The above will work if the event you received is a mouse event, which it is in your example. You can also use Display#getCursorLocation() at any point to get absolute mouse location if you do not have a mouse event at hand.
Following saved my day:
System.out.println("Click: " + parentShell.getDisplay().getCursorLocation());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With