Created
November 21, 2018 23:47
-
-
Save tejashah88/201daacada3a785f86b8cf069ead63f5 to your computer and use it in GitHub Desktop.
Fix for Java's Robot.mouseMove when accounting for High DPI Screens + Mathematical error analysis for modeling how the original mouseMove function is affected.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Robot.betterMouseMove is a function that's meant to fix Robot.mouseMove when dealing with Windows's | |
display scaling in order to make text easier to see. The 'scaleFactor' is the actual scaling factor | |
as a float > 1.0f. | |
This is the error vector field that represents the error caused by Robot.mouseMove in which to move | |
in the x and y direction towards its destination. It's not that useful in hindsight, but kind of cool | |
that one can model the error as a mathematical function. | |
"Error" Vector Field: F(x, y) = f(x, y) * i + g(x, y) * j | |
f(x, y) = (-x + t_x) / (width - t_x) | |
g(x, y) = (-y + t_y) / (height - t_y) | |
x = x coordinate of origin point | |
y = y coordinate of origin point | |
width = width of screen | |
height = height of screen | |
t_x = x coordinate of target point | |
t_y = x coordinate of target point | |
- Note: f(x, y) and g(x, y) will return the negative error result, which represents the | |
"restoring" error quantity required to reach the target. | |
-f(x, y) = actual error amount for x direction | |
-g(x, y) = actual error amount for y direction | |
s = scaling factor of screen (i.e. 125% => s = 1.25) | |
G(x, y) = predicted output coords (as a vector) | |
G(x, y) = ((-x + t_x) * s) * i + (2 * t_y) * ((-y + t_y) * s) * j | |
Note: (2 * t_y) is needed to translate from cartesian coords to screen coords | |
*/ | |
val Robot.defaultScaleFactor: Float | |
get() = 1.0f | |
fun Robot.betterMouseMove(x: Int, y: Int, scaleFactor: Float = this.defaultScaleFactor) { | |
val modFactor = 1 - (1 / scaleFactor) | |
val origin = MouseInfo.getPointerInfo().location | |
val deltaX = x - origin.x | |
val deltaY = y - origin.y | |
val finalX = (x - deltaX * modFactor).toInt() | |
val finalY = (y - deltaY * modFactor).toInt() | |
this.mouseMove(finalX, finalY) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment