8

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.

    -
Lucas
  • 643
  • 1
  • 9
  • 21
  • For yor information that namespace is part of WPF. Have you tried setting a boolean variable to true when you enter Grab and False when you enter Release ? – Mark Hall Dec 19 '11 at 13:10
  • Sneaky WPF, thanks Mark, have adjusted the question. I thought about the bool approach but thought there might be something more elegant. If I / Anyone can't think of another approach it will work, just trying to avoid always taking the easy way out :) – Lucas Dec 19 '11 at 13:21
  • The declaration is wrong, last argument is IntPtr. Pass IntPtr.Zero. – Hans Passant Dec 19 '11 at 15:31
  • I bet this is for auto spray control in csgo This exactly what I need anyway – rrswa May 10 '17 at 08:14

3 Answers3

3

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);
    }
Lucas
  • 643
  • 1
  • 9
  • 21
0
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    private static extern void mouse_event(uint dwFlags, int dx, int dy, uint cButtons, uint dwExtraInfo);

    [DllImport("user32.dll")]
    static extern bool SetCursorPos(int X, int Y);

    const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    const uint MOUSEEVENTF_LEFTUP = 0x0004;
    const uint MOUSEEVENTF_MOVE = 0x0001;

    static void Drag(int startX,int startY,int endX,int endY)
    {
        endX = endX - startX;
        endY = endY - startY;
        SetCursorPos(startX, startY);
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        mouse_event(MOUSEEVENTF_MOVE, endX, endY, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }
0

The following code should return true if the left mouse button is down, false if it is up, Control being System.Windows.Forms.Control:

    Control.MouseButtons.HasFlag(MouseButtons.Left)

p.s. documentation for this can be found on MSDN here.

Gareth
  • 2,746
  • 4
  • 30
  • 44