Home > Enterprise >  Moving mouse curser increment by increment between 2 points on screen
Moving mouse curser increment by increment between 2 points on screen

Time:05-18

Iam using a project that uses mouse drivers to send mouse inputs, for example

from ctypes import *
import time



dd_dll = windll.LoadLibrary('x:\DD94687.64.dll')
time.sleep(2)

st = dd_dll.DD_btn(0) #DD Initialize
if st==1:
    print("OK")
else:
    print("Error")
    exit(101)


#Second argument = positive numbers moves the mouse down, Negative numbers moves the mouse up
#First argument = positive numbers moves the mouse right, negative numbers moves the mouse left

#This will move the curser 5 pixels to the right, and 10 pixels down
def dd_dll.DD_movR(5,10)

it can be found here, i want to calculate and move the curser increment by increment (5 pixels each to be specific) from the center of screen (starting point) to another point specified by the user (it could be any coordinates on screen).

iam using the DD_movR() function, there's another function which is DD_mov() that moves the curser to a specified X,Y position, but for some reason it magnifies any integer given by 0.25. so if i wanted to move the curser to (625,625) my input should be (500,500) . i want to use either of them as long as i move the mouse increment by increment instead of jumping the specified location. I know that there're other methods/libraries that does the same function but the target iam sending mouse inputs to blocks all of them and i found this to be working.

CodePudding user response:

So, you want to determine the next position of the cursor that you will move it to.

Let's imaging we have a straight line connecting the source point and the destination point (in your particular question, the source point is the middle of the screen, but it doesn't really matter which point is selected as the starting point). The next position of the cursor should be approximately aligned with that line; and, it should have the x-position differ from the previous point by 5 pixels.

Thus, it is fairly clear to compute the x-position of the next point. Now you should make use of the other idea (next point is aligned with the straight line) to compute its y-position. How should you do that?

To make use of the straight line, you have to know what it is: turns out it's trivial to derive an equation for that line from the source and destination point. You can refer to this article https://www.mathsisfun.com/algebra/line-equation-2points.html

Note that you already have the x-position. Simply plug it into that line equation, you'll get the y-position (floored to integer). That would be the next y-position.

Once the position of the next point is computed, if you want to use DD_movR, simply do a subtraction to find the difference between the next point and the previous point.

Also, you should check for one corner case: when the next point exceeds the destination point. To resolve this, you can discard the computed next point and use the destination point instead. Your cursor-moving procedure should also end here.


This is the Python code for the above idea. There are still some certain corner cases to be handled.

#source = (x1, y1) : the current position of the cursor
#destination = (x2, y2): the position we want to move the cursor to at the end

def nextX(x1, x2):
    if (x2 >= x1   5):
        return x1   5
    elif (x2 >= x1):
        return x2
    elif (x2 < x1 - 5):
        return x1 - 5
    else:
        return x2

def slope(x1, y1, x2, y2):
    return (y2 - y1) / (x2 - x1) #TODO: need to handle corner case where x1 == x2

#equation will be y - y1 = slope * (x - x1)
#or equivalently y = slope * x   y1 - slope * x1

def nextY(x1, y1, x2, y2):
    eqSlope = slope(x1, y1, x2, y2)
    y = eqSlope * nextX(x1, x2)   y1 - eqSlope * x1
    return round(y)
    
def move(x1, y1, x2, y2):
    nextPosX = nextX(x1, x2)
    nextPosY = nextY(x1, y1, x2, y2)
    dd_dll.DD_movR(nextPosX - x1, nextPosY - y1)
  • Related