Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on an app to automate some input into another application. And i'm running into a problem. Below is the function code i'm using

C#
public class MouseClick
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
}

public enum MouseButton
{
    MOUSEEVENTF_LEFTDOWN = 0x02,
    MOUSEEVENTF_LEFTUP = 0x04,
    MOUSEEVENTF_RIGHTDOWN = 0x08,
    MOUSEEVENTF_RIGHTUP = 0x10
}

and here is the code i'm using to move and click

C#
Point LocPoint = GetLocation(Column, Row, Item);
                                Console.WriteLine("Column: {0}\tRow: {1}\tItem: {2}\tPoints: {3}\tCursor: {4}", Column, Row, Item, Points, LocPoint.X + "," + LocPoint.Y);

                                Thread.Sleep(200);
                                Cursor.Position = LocPoint;
                                Thread.Sleep(10);
                                MouseClick.mouse_event((int)MouseButton.MOUSEEVENTF_LEFTDOWN | (int)MouseButton.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                                Thread.Sleep(200);

However here is where things get interesting when I don't have the application I want to input into as the active window but say mspaint the code runs fine and I get dots from the paint brush where I want to click however when the application I want to run this in is active the mouse doesn't ever move and no click is registered it is as if the application is intercepting these calls and ignoring them. So this leads me to two questions

1.) Is this possible? How does it detect difference between a mouse and setting the cords
2.) Is there a way around this or another method to use?

___Update_____________________________________

So everyone is saying to use SendInput instead so here is my update. Ok so I changed the code to use SendInput. I've also tried C# SendKeys as a test as well. Currently I've gone back to the basic and i'm just trying to input the letter A into a text input box that I make the target manually. When I run it in Notepad both SendInput and SendKeys both type the letter A however when i'm inside the other application i'm trying to automate this to nothing shows up. Here is the SendInput code i'm using.

C#
INPUT[] Inputs = new INPUT[2];
Inputs[0].type = WindowsAPI.INPUT_KEYBOARD;
Inputs[0].ki.wVk = 0;
Inputs[0].ki.dwFlags = WindowsAPI.KEYEVENTF_UNICODE;
Inputs[0].ki.wScan = 0x41;

Inputs[0].type = WindowsAPI.INPUT_KEYBOARD;
Inputs[0].ki.wVk = 0;
Inputs[1].ki.dwFlags = WindowsAPI.KEYEVENTF_KEYUP;
Inputs[0].ki.wScan = 0x41;

WindowsAPI.SendInput((uint)Inputs.Length, Inputs, Marshal.SizeOf(Inputs[0]));
Posted
Updated 29-May-14 4:54am
v2
Comments
gggustafson 28-May-14 16:52pm    
Are you familiar with User Interface Privilege Isolation (UIPI)?
Sergey Alexandrovich Kryukov 28-May-14 17:29pm    
To start with, use SendInput instead of mouse_event. At least you would not use obsolete API which has been superseded by newer one.
—SA

First thing I can see is the absurd use of mouse_event: setting left down and left up at the same time. No, it does not mean emulating click. It might work somehow, but only by some assumed fool-proof behavior of the system, but I would not guarantee anything. To emulate click, you would need to emulate MOUSEEVENTF_LEFTDOWN if one call and then MOUSEEVENTF_LEFTUP in another call. Just do it.

Now, if you want to emulate using a brush tool in a image editor, you need to click on the tool, move mouse in the drawing area and 1) simulate left mouse key down, 2) simulate mouse move. Then it should make a brush stroke.

And please see my comment to the question: use SendInput instead:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^],
http://www.pinvoke.net/default.aspx/user32.sendinput[^].

As you can see from the second link, you don't even have to write P/Invoke code by yourself; it is already done for you.

Look, this API is very low level, it works as it is called from a device driver; its operation does not depend on applications receiving events. It just works like a real mouse device. If you use this emulation correctly it would behave like a real mouse device and won't create any problems.

—SA
 
Share this answer
 
Comments
Cpajoe2001 29-May-14 10:48am    
Ok so I changed the code to use SendInput. I've also tried C# SendKeys as a test as well. Currently I've gone back to the basic and i'm just trying to input the letter A into a text input box that I make the target manually. When I run it in Notepad both SendInput and SendKeys both type the letter A however when i'm inside the other application i'm trying to automate this to nothing shows up. Here is the SendInput code i'm using.

INPUT[] Inputs = new INPUT[2];
Inputs[0].type = WindowsAPI.INPUT_KEYBOARD;
Inputs[0].ki.wVk = 0;
Inputs[0].ki.dwFlags = WindowsAPI.KEYEVENTF_UNICODE;
Inputs[0].ki.wScan = 0x41;

Inputs[0].type = WindowsAPI.INPUT_KEYBOARD;
Inputs[0].ki.wVk = 0;
Inputs[1].ki.dwFlags = WindowsAPI.KEYEVENTF_KEYUP;
Inputs[0].ki.wScan = 0x41;

WindowsAPI.SendInput((uint)Inputs.Length, Inputs, Marshal.SizeOf(Inputs[0]));
Sergey Alexandrovich Kryukov 29-May-14 10:54am    
First of all, SendKeys is nearly useless. For your purpose, just forget it. Now you shifted discussion from mouse events to keys, why? Any problem with that?
How about your original question? Did you achieve mouse simulation? It should not be a problem.
—SA
So after just frustration I decided to run the app outside of Visual Studio's debug mode which resulted in the same result nothing. However I decided to run the app "As Administrator" even through I have UAC turned all the down and to my surprise the application properly moved the mouse, clicked and inputted text. I do not know why this is required as I've pinvoked methods before and never had to do this however that seems to be the solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900