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:
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?
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().getScreenDevices(), and their configurations with .getConfigurations(), but I idk how to choose from that array.