C# SendInput在pc的水果忍者中被阻止

C# SendInput在pc的水果忍者中被阻止,c#,sendinput,C#,Sendinput,我正在编写一个程序,它使用SendInput函数模拟鼠标事件来为PC玩水果忍者。但是当我调用SendInput时,鼠标移动了,但游戏屏幕上什么也没有发生。有人能告诉我为什么它不起作用,并给我一些解决方案吗?我在C#上编程,并使用[dllimport]调用SendInput函数。 谢谢。我建议您只模拟鼠标移动。您应该模拟鼠标拖动。首先做鼠标向下移动->鼠标移动->鼠标向上移动 下面是我以前项目中的一些代码: namespace Clicker.Enums { public enum Mou

我正在编写一个程序,它使用
SendInput
函数模拟鼠标事件来为PC玩水果忍者。但是当我调用
SendInput
时,鼠标移动了,但游戏屏幕上什么也没有发生。有人能告诉我为什么它不起作用,并给我一些解决方案吗?我在C#上编程,并使用
[dllimport]
调用
SendInput
函数。
谢谢。

我建议您只模拟
鼠标移动
。您应该模拟
鼠标拖动
。首先做
鼠标向下移动
->
鼠标移动
->
鼠标向上移动

下面是我以前项目中的一些代码:

namespace Clicker.Enums
{
    public enum MouseEvents
    {
        MOVE = 0x0001, /* mouse move */
        LEFTDOWN = 0x0002, /* left button down */
        LEFTUP = 0x0004, /* left button up */
        RIGHTDOWN = 0x0008, /* right button down */
        RIGHTUP = 0x0010, /* right button up */
        MIDDLEDOWN = 0x0020, /* middle button down */
        MIDDLEUP = 0x0040, /* middle button up */
        XDOWN = 0x0080, /* x button down */
        XUP = 0x0100, /* x button down */
        WHEEL = 0x0800, /* wheel button rolled */
        VIRTUALDESK = 0x4000, /* map to entire virtual desktop */
        ABSOLUTE = 0x8000 /* absolute move */
    }
}

namespace Clicker.Actions
{
    public class Action
    {        
        protected int x;
        protected int y;

        ...

        [DllImport("user32")]
        protected static extern int SetCursorPos(int x, int y);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        protected static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        ...

        public int X { get { return x; } set { x = value; } }
        public int Y { get { return y; } set { y = value; } }

        ...

        public vritual void PerformAction()
        {

        }

        ...
    }
}

namespace Clicker.Actions
{
    public class Drag : Action
    {
        public int To_X { get; set; }
        public int To_Y { get; set; }

        ...        

        public override void PerformAction()
        {
            SetCursorPos(X, Y);
            Thread.Sleep(100);
            mouse_event((int)MouseEvents.LEFTDOWN, 0, 0, 0, 0);
            SetCursorPos(To_X, To_Y);
            Thread.Sleep(100);
            mouse_event((int)MouseEvents.LEFTUP, 0, 0, 0, 0);
        }

        ...
    }
}
使用:

Action sliseFruit = new Drag(){ 
    X = 0, // from (X=0, Y=0)
    Y = 0, // from (X=0, Y=0)
    To_X = 100, // to (X=100, Y=100)
    To_Y = 100, // to (X=100, Y=100)
};

sliseFruit.PerformAction();
这里还有一些建议:如果您想在应用程序的主窗体之外单击,问题也可能出现在
SynchronizationContext
和/或线程中。可能的解决办法:

namespace Clicker.Actions
    {
        public class Action
        {        
            protected int x;
            protected int y;

            // Add this fields
            private static SynchronizationContext _context = null;
            public static SynchronizationContext Context { get { return _context; } set { _context = value; } }

            ...

            [DllImport("user32")]
            protected static extern int SetCursorPos(int x, int y);

            [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            protected static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

            ...

            public int X { get { return x; } set { x = value; } }
            public int Y { get { return y; } set { y = value; } }

            ...

            // ... and this method
            internal virtual void InnerPerformAction(object state)
            {
                 return;
            }

            // change PerformAction like this
            public void PerformAction()
            {
                InnerPerformAction(new object());
            }

            ...
        }
    }
现在编辑拖动动作类:

namespace Clicker.Actions
    {
        public class Drag : Action
        {
            public int To_X { get; set; }
            public int To_Y { get; set; }

            ...        

            internal override void InnerPerformAction(object state)
            {
               try
               {
                   Context.Send(new SendOrPostCallback(delegate
                       {
                           SetCursorPos(X, Y);
                           Thread.Sleep(100);
                           mouse_event((int)MouseEvents.LEFTDOWN, 0, 0, 0, 0);
                           SetCursorPos(To_X, To_Y);
                           Thread.Sleep(100);
                           mouse_event((int)MouseEvents.LEFTUP, 0, 0, 0, 0);
                       }), state);

               }
               catch
               {
                   //Aaaaahh... what ever...
               }
            }

            ...
        }
    }
差点忘了:

// You have to do this in your main form constructor
Action.Context = SynchronizationContext.Current;

我刚刚用这段代码创建了一个简单的解决方案,效果很好。如果您需要,我可以通过电子邮件或其他方式发送给您。

是的。我对鼠标拖动进行了编码。尽管我也在spy++上测试了windows消息,但它还是不起作用。真实爆炸和虚拟爆炸都有相同的发送消息和参数。我使用了你的代码,但仍然不起作用。顺便说一句,为什么没有任何MouseEvents.MOVE消息被发送?@user2711463,因为
保护了静态外部intsetcursorpos(intx,inty)被使用。我希望我的程序按照如下方式工作:当我指定一条线的两个端点的坐标时,程序会自动在水果忍者游戏屏幕中爆炸。到目前为止,鼠标图标只是在屏幕上移动,我的程序无法与游戏交互。您的代码看起来像我在CodeProject上引用的项目。我的程序是通过一些编辑创建的。除了我想自动玩的游戏外,它几乎适用于所有程序。