Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Would I Set Mouse Cursor At X,Y Coordinates, Click Left Mouse And Scroll Left,Right,Top,Bottom

Tags:

c#

As described in the title I've been trying to search for a way to set the mouse coordinates by using a Cursor.Position = new Point(58, 128); Then while holding the Left Mouse button (Down) I'm trying to scroll to another direction(random direction). For example, if I was to go onto Google Earth and set the cursor position at 0,0 the cursor would then scroll around the map. If anyone can help out I would surely appreciate it.

Thanks

Solution: floatas, thanks again for responding to this post. I spent yesterday and today trying to figure this out and I finally got it working. I will post my code in hopes this helps others out.

like image 825
Jordan Miller Avatar asked Nov 09 '22 10:11

Jordan Miller


1 Answers

First of all you will need to import some functions.

To change cursor position:

   [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetCursorPos(
        [In] int X,
        [In] int Y);

To simulate mouse events:

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(
    [In] uint dwFlags,
    [In] uint dx,
    [In] uint dy,
    [In] int dwData,
    [In] uint dwExtraInfo);

Possible mouse events:

public enum MouseEvents
{
    MOUSEEVENTF_LEFTDOWN = 0x02,
    MOUSEEVENTF_LEFTUP = 0x04,
    MOUSEEVENTF_RIGHTDOWN = 0x08,
    MOUSEEVENTF_RIGHTUP = 0x10,
    MOUSEEVENTF_WHEEL = 0x0800,
}

You can send mouse down and mouse up, simulating click:

mouse_event((uint)MouseEvents.MOUSEEVENTF_LEFTDOWN | (uint)MouseEvents.MOUSEEVENTF_LEFTUP, X, Y, 0, 0);

Didn't tested this, but should press mouse, drag and release:

mouse_event((uint)MouseEvents.MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);
SetCursorPos((int)X+10, (int)Y+10);
mouse_event((uint)MouseEvents.MOUSEEVENTF_LEFTUP, X+10, Y+10, 0, 0);
like image 65
floatas Avatar answered Nov 14 '22 22:11

floatas