473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Simulating mouse movement

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:

//------------------------------------
// START CODE SNIPPIT
//------------------------------------

// in the code when I want to move the mouse I run this:
// (assuming i want to move the mouse to 500px,500px:
moveMouseTo(500,500);
// the mouse then moves 500 in the x direction but 0 in the y?
//Constants
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());
User32.mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENT F_MOVE, x, y, 0,
(System.IntPtr)0);
}

private class User32 {
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}

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

//------------------------------------
// END CODE SNIPPIT
//------------------------------------
Nov 17 '05 #1

✓ answered by Benny Raymond

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

User32.SetCursorPos(X, Y);

grabbed from:

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

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

User32.SetCursorPos(X, Y);

grabbed from:

[DllImport("user32.dll", SetLastError=true)]
[return:MarshalAs(UnmanagedType.Bool)]
public static extern bool SetCursorPos(int X, int Y);
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: BadOmen | last post by:
I want my program to send a mouse click to the window at the current mouse position, how do I do that? Example: I have my mouse over a button in Word and then my program is sending the left...
10
by: Danny | last post by:
How can I get the coordinates of the mouse cursor in Mozilla browsers as well as Opera and IE6? I'm struggling to understand how to capture mouse movement events with Mozilla/Netscape/Firefox and...
3
by: Wapr | last post by:
Hi, How can I limit to movement of the cursor on my form to a specified rectangle? thanks
6
by: Carlos García-Carazo | last post by:
Hello, I am working on a C# application for an industrial machine, using Windows Forms, where the user could look at the screen from a 90 degree rotated position, like he could turn the monitor...
4
by: mike | last post by:
I have the opportunity to rescue a project that uses a mouse to sense the relative position of a machine. The hardware is built...just needs to be programmed. Stop snickering!!! I didn't do it...I...
5
by: hurricane_number_one | last post by:
I am creating a simple server application, that will listen for incoming mouse coordinates and then move the mouse accordingly. So basically it's like a very simple VNC server without and screen...
6
by: c.k. | last post by:
I was wondering if there was a way to give mouse movement on an empty form some lag/delay? For instance, user moves mouse, and the cursor is 20ms (possible adjustable from some option) behind the...
0
by: Studlyami | last post by:
I am trying to find how windows translate the input from a HID device to an actual windows message; specifically, the mouse. I am trying to figure out how when the mouse moves the process windows...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.