Trying to simulate a mouse click / drag

So, I'm trying to simulate a left mouse click and a left mouse button to do auto drag and drop.

It is currently located in C # Winforms (Yes, winforms: |) and is a bit scary.

Basically, after sending the click, I want it to update the cursor position based on 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 is the code I'm using now + some psuedocode to better explain myself (do it now).

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 had a google hunt and I can’t find anything I need but the WPF equivalent: http://msdn.microsoft.com/en-us/library/system.windows.input.mouse.aspx

I'm a little out of my depth, but any help is much appreciated.

Lucas.

  • -
+4
source share
3 answers

The simplest answer has been spoiled to use bool and just check what happens.

I started it in a new stream so that it would not break everything else.

Ideally, you would save that a 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); } 
+2
source

The following code should return true if the left mouse button is not specified, false if it is installed, Control - System.Windows.Forms.Control:

  Control.MouseButtons.HasFlag(MouseButtons.Left) 

ps documentation for this can be found on MSDN here .

0
source
 [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
source

Source: https://habr.com/ru/post/1387035/


All Articles