kinectv2来控制鼠标

时间:2016-02-25 02:02:25

标签: c# mouse kinect

我使用kinectv2在windows store app中用手势控制鼠标。(当右手关闭时,鼠标点击).I使用mouse_event和SentInput方法。但它不起作用。我的操作系统是Windows 10教育。当我在OS Windows 8.1上执行该项目时,它的工作原理。为什么?

    [DllImport("user32.dll")]
    public static extern bool SetCursorPos(int X, int Y);
    [DllImport("user32.dll")]
    static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
    [Flags]
    enum MouseEventFlag : uint
    {
        Move = 0x0001,
        LeftDown = 0x0002,
        LeftUp = 0x0004,
        RightDown = 0x0008,
        RightUp = 0x0010,
        MiddleDown = 0x0020,
        MiddleUp = 0x0040,
        XDown = 0x0080,
        XUp = 0x0100,
        Wheel = 0x0800,
        VirtualDesk = 0x4000,
        Absolute = 0x8000
    }


   private void UpdateBody(Body body, int bodyIndex)
    {
        coordinateMapper = this.kinectSensor.CoordinateMapper;
        foreach (var jointType in body.Joints.Keys)
        {
            CameraSpacePoint position = body.Joints[jointType].Position;
            if (position.Z < 0)
            {
                position.Z = InferredZPositionClamp;
            }
            DepthSpacePoint depthSpacePoint = coordinateMapper.MapCameraPointToDepthSpace(position);
            handCoordinate[0] = depthSpacePoint.X;
            handCoordinate[1] = depthSpacePoint.Y;

            if (jointType == JointType.HandRight)
            {
                if (!Double.IsInfinity(depthSpacePoint.X) && !Double.IsInfinity(depthSpacePoint.Y))
                {
                    SetHandElipse(handPoint, depthSpacePoint.X, depthSpacePoint.Y);
                    //mouse_event(MouseEventFlag.Move | MouseEventFlag.Absolute, (int)depthSpacePoint.X*100, (int)depthSpacePoint.Y*100, 0, UIntPtr.Zero);
                    SetCursorPos((int)depthSpacePoint.X,(int)depthSpacePoint.Y);//move the mouse
                    System.Threading.Tasks.Task.Delay(500);
                    if (body.HandRightState == HandState.Closed&&mark)
                    {
                        mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);//click
                        Task.Delay(TimeSpan.FromSeconds(1));
                        mark = false;
                    }
                }

            }
        }
    }

0 个答案:

没有答案