473,385 Members | 1,720 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,385 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 19002
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.