473,881 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ABS OLUTE = 0x8000;
private const UInt32 MOUSEEVENTF_MOV E = 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_ev ent(MOUSEEVENTF _ABSOLUTE|MOUSE EVENTF_MOVE, x, y, 0,
(System.IntPtr) 0);
}

private class User32 {
[StructLayout(La youtKind.Sequen tial)]
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}

[DllImport("user 32.dll")]
public static extern IntPtr GetDesktopWindo w();
[DllImport("user 32.dll")]
public static extern IntPtr GetWindowDC(Int Ptr hWnd);
[DllImport("user 32.dll")]
public static extern IntPtr ReleaseDC(IntPt r hWnd, IntPtr hDC);
[DllImport("user 32.dll")]
public static extern IntPtr GetWindowRect(I ntPtr hWnd, ref RECT rect);
[DllImport("user 32.dll")]
public static extern void mouse_event(uin t dwFlags, long dx, long dy,
uint dwData, IntPtr dwExtraInfo);
[DllImport("user 32.dll")]
public static extern void keybd_event(byt e bVk, byte bScan, uint
dwFlags, IntPtr dwExtraInfo);
[DllImport("user 32.dll")]
private static extern bool RegisterHotKey( IntPtr hWnd, int id, uint
modifiers, uint vk);
[DllImport("user 32.dll")]
private static extern bool UnregisterHotKe y(IntPtr hWnd, int id);
[DllImport("kern el32.dll")]
private static extern int GlobalAddAtom(S tring atomString);
[DllImport("kern el32.dll")]
private static extern int GlobalDeleteAto m(int atom);
}

//------------------------------------
// END CODE SNIPPIT
//------------------------------------
Nov 17 '05 #1
1 19293
Nevermind... Just figured out there's another function in User32 called:

User32.SetCurso rPos(X, Y);

grabbed from:

[DllImport("user 32.dll", SetLastError=tr ue)]
[return:MarshalA s(UnmanagedType .Bool)]
public static extern bool SetCursorPos(in t 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
32667
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 mouse click and the button under the mouse is clicked. Yours, Jonas
10
18097
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 I've Googled so much my brain hurts. http://www.ghpkendal.co.uk/TestPages/Test.htm Move your cursor over the yellow area and you should see the mouse coordinates echoed above.
3
2242
by: Wapr | last post by:
Hi, How can I limit to movement of the cursor on my form to a specified rectangle? thanks
6
10824
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 to leave it standing on its left (or right) side. There are many ways to show a "turned" interface in this case, but the problem is that when he moves the mouse right, in a turned screen the mouse cursor goes down (or up), when he moves it up...
4
6984
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 just gotta fix it. I need to make some calculations on the measurements and VB6 is my language. Yes, the system mouse will corrupt the measurement, but it's an auditing function and that's acceptable.
5
2339
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 display. I have this basic part working. The problem is that response time is really bad. It seems like the server is not receiving the data fast enough to be able to move the mouse so that it appears to be in sync with the movement on the client...
6
3124
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 movement. So when user stops moving mouse, 20ms later the cursor itself stops moving... I tried using: Windows.Forms.Cursor.Current.Position = New System.Drawing.Point(x,y) and forms of sleep/timer events but cant seem to get it to work...
0
2318
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 takes in order to ensure all windows receive the notification that the mouse moved and how windows moves the mouse cursor. What process handles the movement of the mouse cursor and at what point does windows move the mouse cursor vs posting the...
0
9928
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11100
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10401
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7953
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7110
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.