Cursor.Postition和Mouse_Event被阻止 [英] Cursor.Postition and Mouse_Event being Blocked

查看:130
本文介绍了Cursor.Postition和Mouse_Event被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,以自动将某些输入输入到另一个应用程序中.而且我遇到了问题.以下是我使用的功能代码

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

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

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);


但是在这里,当我没有要输入的应用程序作为活动窗口时,事情变得很有趣,但是说mspaint代码运行正常,并且我从画笔中获得了点,但是当我在应用程序中单击时,想要在运行中运行此鼠标,它永远不会移动,并且没有注册任何点击,就好像应用程序正在拦截这些调用并忽略它们一样.所以这引出了两个问题

1.)这可能吗?如何检测鼠标和设置电源线之间的差异
2.)有没有其他方法可以使用?

___Update_____________________________________

所以每个人都说使用SendInput代替,所以这是我的更新.好的,所以我将代码更改为使用SendInput.我也尝试过使用C#SendKeys作为测试.目前,我已经回到基础上,我只是想将字母A输入到我手动设置目标的文本输入框中.当我在记事本中运行它时,SendInput和SendKeys都键入字母A,但是当我在另一个应用程序中时,我试图自动执行此操作,则不会显示任何内容.这是我正在使用的SendInput代码.


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.

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]));

推荐答案

我首先看到的是mouse_event的荒谬用法:同时设置左下和左上.不,这并不意味着模拟点击.它可能会以某种方式工作,但是只能通过系统假定的一些傻瓜行为来实现,但是我不能保证任何事情.要模拟点击,如果一个呼叫,则需要模拟MOUSEEVENTF_ LEFTDOWN,然后在另一个呼叫中,则模拟MOUSEEVENTF_LEFTUP.就做吧.

现在,如果要在图像编辑器中使用画笔工具进行仿真,则需要单击该工具,在绘图区域中移动鼠标,然后1)模拟鼠标左键向下; 2)模拟鼠标移动.然后应该做出笔触.

并且请查看我对问题的评论:改为使用SendInput:
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms646310%28v=vs.85%29.aspx [ http://www.pinvoke.net/default.aspx/user32.sendinput [ ^ ].

从第二个链接可以看到,您甚至不必自己编写P/Invoke代码.已经为您完成了.

看,这个API是非常低级的,可以像从设备驱动程序中调用一样工作.它的操作不依赖于接收事件的应用程序.它就像一个真正的鼠标设备一样工作.如果正确使用此仿真,它将像真正的鼠标设备一样工作,不会造成任何问题.

—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


因此,在无奈之后,我决定在Visual Studio的调试模式之外运行该应用程序,结果一无所获.但是,即使通过UAC,我也决定通过以管理员身份"运行该应用程序,但令我惊讶的是,该应用程序正确地移动了鼠标,单击并输入了文本.我不知道为什么这是必需的,因为我以前曾使用过方法,并且从来不需要这样做,但这似乎是解决方案.
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.


这篇关于Cursor.Postition和Mouse_Event被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆