Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempting to Simulate Mouse Click / Drag

So I'm trying to simulate the left mouse click and the left mouse release to do some automated dragging and dropping.

It's currently in a C# Winforms (Yes, winforms :|) and is being a bit of a goose.

Basically, once a Click is sent, I want it to update the cursor position based upon the Kinect input. The Kinect side of things is fine but i'm not sure how to find if the button is still pressed or not.

here's the code i'm currently using + some psuedocode to help better explain myself (the do while).

class MouseImpersonator
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

    private const int leftDown = 0x02;
    private const int leftUp = 0x04;

    public static void Grab(int xPos, int yPos)
    {
        Cursor.Position = new Point(xPos + 25, yPos + 25);
        mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);

        //do
        //{
        //Cursor.Position = new Point(KinectSettings.movement.LeftHandX, KinectSettings.movement.LeftHandY);
        //} while (the left mouse button is still clicked);
    }

    public static void Release(int xPos, int yPos)
    {
        Cursor.Position = new Point(xPos + 25, yPos + 25);
        mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
    }
}

I've had a hunt of the google and can't find anything for what I need except for a WPF equivalent: http://msdn.microsoft.com/en-us/library/system.windows.input.mouse.aspx

I'm a bit out of my depth, but any help is greatly appreciated.

Lucas.

    -
like image 451
Lucas Avatar asked Dec 19 '11 13:12

Lucas


1 Answers

The Easiest answer was infact to use a bool and just check to see what's going on.

I started it on a new thread so it didn't break everything else.

Idealy you'd tidy this up a little bit.

    public static void Grab(int xPos, int yPos)
    {
        _dragging = true;

        Cursor.Position = new Point(xPos, yPos + offSet);
        mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);

        var t = new Thread(CheckMouseStatus);
        t.Start();
    }
    public static void Release(int xPos, int yPos)
    {
        _dragging = false;
        Cursor.Position = new Point(xPos, yPos + offSet);
        mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
    }

    private static void CheckMouseStatus()
    {
        do
        {
            Cursor.Position = new Point(KinectSettings.movement.HandX, KinectSettings.movement.HandY + offSet);
        } 
        while (_dragging);
    }
like image 90
Lucas Avatar answered Nov 12 '22 10:11

Lucas