SendInput is blocked in fruit ninja for pc

402 Views Asked by At

I am writing a program that uses SendInput function to simulate the mouse events to play fruit ninja for PC. But when I called SendInput, the mouse moved but nothing happened on the game screen. Could anyone tell me why it doesn't work and give me some solutions? I programmed on C# and used [dllimport] to call SendInput function. Thanks.

1

There are 1 best solutions below

7
On

I suggest You are simulating only mouse move. You should simulate mouse drag. First do mouse down -> mouse move -> mouse up

Here some code from my old project:

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

        ...
    }
}

Using:

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

Here are some more suggestions: Also the issue can be in SynchronizationContext and/or Threading if you want to click outside the main form of your application. Possible solution:

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

            ...
        }
    }

Now edit the Drag action class:

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...
               }
            }

            ...
        }
    }

Almost forgot:

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

I've just created simple solution with this code, and it works fine. If you want I can send you it by email or something.