Simulating mouse movement


Benny Raymond

In my attempt to make a macro recording program where i can then
playback mouse movements i'm running into a big problem:

I'm trying to simulate the movement of the mouse to a point on the
screen. With my current code the mouse is only moving in the X
direction and not in the Y direction (however my debug line is telling
me that it's trying to move in the Y as well). I've read everything I
could find online, tried some hacky ways of getting the mouse to move,
etc... I'm stuck. The following is the part of my code that handles the
mouse movement - I was hoping that someone in here could figure out why
the mouse isn't moving in the Y direction, or if someone knows of an
easier way of moving the mouse around the screen:


// in the code when I want to move the mouse I run this:
// (assuming i want to move the mouse to 500px,500px:
// the mouse then moves 500 in the x direction but 0 in the y?

private const UInt32 MOUSEEVENTF_ABSOLUTE = 0x8000;
private const UInt32 MOUSEEVENTF_MOVE = 0x0001;

//Screen coord conversion factors.
private const double SCREEN_X_CONV = 64.0; //65535 / 1024
private const double SCREEN_Y_CONV = 85.3; //65535 / 768

public void moveMouseTo(int x, int y) {
x = (int) (x * SCREEN_X_CONV);
y = (int) (y * SCREEN_Y_CONV);
Debug.WriteLine("mouse to: " + x.ToString() + "," + y.ToString());

private class User32 {
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;

public static extern IntPtr GetDesktopWindow();
public static extern IntPtr GetWindowDC(IntPtr hWnd);
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
public static extern void mouse_event(uint dwFlags, long dx, long dy,
uint dwData, IntPtr dwExtraInfo);
public static extern void keybd_event(byte bVk, byte bScan, uint
dwFlags, IntPtr dwExtraInfo);
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint
modifiers, uint vk);
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private static extern int GlobalAddAtom(String atomString);
private static extern int GlobalDeleteAtom(int atom);


Benny Raymond

Nevermind... Just figured out there's another function in User32 called:

User32.SetCursorPos(X, Y);

grabbed from:

[DllImport("user32.dll", SetLastError=true)]
public static extern bool SetCursorPos(int X, int Y);

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question