1

In many multimonitor setups featuring monitors of varying stand heights, the vertical positions of the monitors can be adjusted in software so that their graphics align correctly. A vertical displacement is added to the Windows mouse coordinates to give this effect; shown in purple in the diagram: enter image description here I am building a program that needs to know when the user's mouse has reached the top of any of their monitors. I tried using MouseInfo.getPointerInfo().getLocation().y==0, but this will not work when the monitors have been displaced in software because the top of the monitor may not always be zero.

Is there a reliable and efficient way to identify this offset across multiple displays?

Syd Lambert
  • 1,415
  • 14
  • 16
  • 1
    Haven't tested it, but according to the docs, [`GraphicsConfiguration.getBounds()`](https://docs.oracle.com/javase/8/docs/api/java/awt/GraphicsConfiguration.html#getBounds--) sounds like what you're looking for. You should be able to get all monitors with [`GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()`](https://docs.oracle.com/javase/8/docs/api/java/awt/GraphicsEnvironment.html), and their configurations with [`.getConfigurations()`](https://docs.oracle.com/javase/8/docs/api/java/awt/GraphicsDevice.html#getConfigurations--), but I idk how to choose from that array. – Siguza Oct 29 '16 at 19:55
  • @Siguza Thanks for your comment, it fully answered my question and I was able to accomplish what I was trying to do. Would you be able to write that in answer format so I can give you proper credit? – Syd Lambert Oct 29 '16 at 22:51
  • To be honest, I don't feel like changing my monitor setup in order to verify the correctness of the answer I'd write, which is why I didn't make it an answer in the first place. Feel free to create your own answer with that info though, I'm just happy to help. – Siguza Oct 30 '16 at 14:33
  • @Siguza Thanks :) – Syd Lambert Oct 30 '16 at 18:27

1 Answers1

1

Thanks to @Siguza's comment, I was able to do what I was trying to accomplish. Simply put, you can find the Rectangle object for the monitor that the mouse is on by using this code: MouseInfo.getPointerInfo().getDevice().getDefaultConfiguration().getBounds()

The Rectangle can also be found for each monitor using the monitor's numerical index within Windows. The code GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDefaultConfiguration().getBounds() will return the Rectangle for the main monitor (index 0), other monitors can be accessed by changing the index of the GraphicsDevice

The vertical displacement of the monitor is in the .y property of the Rectangle object; this value can be used to identify the topmost coordinate of the monitor.

@Siguza's Comment:

Haven't tested it, but according to the docs, GraphicsConfiguration.getBounds() sounds like what you're looking for. You should be able to get all monitors with GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenD‌​evices(), and their configurations with .getConfigurations(), but I idk how to choose from that array.

Syd Lambert
  • 1,415
  • 14
  • 16