Esempio n. 1
0
    public override void OnFrame(Controller controller)
    {
        Frame currentFrame = controller.Frame();

        currentTime = currentFrame.Timestamp;
        changeTime  = currentTime - prevTime;
        if (changeTime > 5000)
        {
            Hand        hand          = currentFrame.Hands[0];
            FingerList  fingers       = hand.Fingers;
            Pointable   pointable     = currentFrame.Pointables[0];
            Leap.Screen screen        = controller.LocatedScreens.ClosestScreenHit(pointable);
            Frame       prevFrame     = controller.Frame(10);
            Hand        prevhand      = prevFrame.Hands[0];
            FingerList  prevfingers   = prevhand.Fingers;
            Pointable   prevpointable = prevFrame.Pointables[0];
            ScreenList  screenList    = controller.LocatedScreens;
            Leap.Screen prevscreen    = screenList.ClosestScreenHit(prevpointable);

            if (!fingers.IsEmpty)
            {
                float prevwidth  = prevscreen.Intersect(prevpointable, true, 1.0F).x *prevscreen.WidthPixels;
                float prevheight = prevscreen.Intersect(prevpointable, true, 1.0F).y *prevscreen.HeightPixels;
                float width      = screen.Intersect(pointable, true, 1.0F).x *screen.WidthPixels;
                float height     = screen.Intersect(pointable, true, 1.0F).y *screen.HeightPixels;
                float tranX      = currentFrame.Translation(prevFrame).x;
                float tranY      = currentFrame.Translation(prevFrame).y;
                int   fwidth     = (int)((width * 0.2) + (prevwidth * (1.0 - 0.2)));
                int   fheight    = (int)((height * 0.2) + (prevheight * (1.0 - 0.2)));
                fheight = screen.HeightPixels - fheight;
                if (fingers.Count == 2 || fingers.Count == 3)
                {
                    if (changeTime > 5000)
                    {
                        if (fingers.Count == 2)
                        {
                            mouse_event(0x0002 | 0x0004, 0, fwidth, fheight, 0);
                        }
                        else
                        {
                            mouse_event(0x0008 | 0x0010, 0, fwidth, fheight, 0);
                        }

                        Console.Write("Clicked At " + fwidth + " " + fheight);
                    }
                }
                else
                {
                    if (fingers.Count == 1)
                    {
                        Console.Write("TipPosition: " + fingers[0].TipPosition + " Width: " + width + " height: " + height + " tranX: " + tranX + " tranY: " + tranY + "\n");
                        SetCursorPos(fwidth, fheight);
                    }
                }
            }
            prevTime = currentTime;
        }
    }
    void NormalizeFingerPoint(FingerList fingers, Leap.Screen screen)
    {
        Vector tempNormPoint = screen.Intersect(fingers[0].TipPosition, fingers[0].Direction, true, 10.0F);

        if ((tempNormPoint.x.ToString() == "NaN") || tempNormPoint.y.ToString() == "NaN")
        {
            return;
        }

        normalizedPoint = tempNormPoint;

        if (calibrateDirection)
        {
            Calibration(new Vector2(normalizedPoint.x, normalizedPoint.y));
        }

        //	Calibration(xNormalized,yNormalized);
        normalizedPoint.x = (normalizedPoint.x - avgLeftDownCalibrationPoint.x) / (avgRightUpCalibrationPoint.x - avgLeftDownCalibrationPoint.x);
        normalizedPoint.y = (normalizedPoint.y - avgLeftDownCalibrationPoint.y) / (avgRightUpCalibrationPoint.y - avgLeftDownCalibrationPoint.y);


        if (normalizedPoint.x < 0)
        {
            normalizedPoint.x = 0;
        }
        else if (normalizedPoint.x > 1)
        {
            normalizedPoint.x = 1;
        }
        if (normalizedPoint.y < 0)
        {
            normalizedPoint.y = 0;
        }
        else if (normalizedPoint.y > 1)
        {
            normalizedPoint.y = 1;
        }

        /*
         * var x = (normalizedPoint.x * screen.WidthPixels);
         * var y = screen.HeightPixels - (normalizedPoint.y * screen.HeightPixels);
         * var screenRatios = new Vector(x, y, 0);
         * return screenRatios;
         */
    }
Esempio n. 3
0
        public Vector getFingerVector(Controller controller, Finger finger)
        {
            Vector finger_vector = new Vector();

            // Get the closest screen intercepting a ray projecting from the finger
            Leap.Screen screen = controller.CalibratedScreens.ClosestScreenHit(finger);


            if (screen != null && screen.IsValid)
            {
                Vector screenStabilized = screen.Intersect(finger, true);
                var    xScreenIntersect = screenStabilized.x;
                var    yScreenIntersect = screenStabilized.y;

                if (xScreenIntersect.ToString() != "NaN")
                {
                    finger_vector.x = (int)(xScreenIntersect * screen.WidthPixels);
                    finger_vector.y = (int)(screen.HeightPixels - (yScreenIntersect * screen.HeightPixels * yAxisMultiplier));
                }
                //SafeWriteLine("yScreenIntersect / HeightPixels" + "/" + yScreenIntersect + "/" + screen.HeightPixels);
            }

            return(finger_vector);
        }
Esempio n. 4
0
    public override void OnFrame(Controller controller)
    {
        currentFrame = controller.Frame();
        prevFrame    = controller.Frame(10);
        currentTime  = currentFrame.Timestamp;
        changeTime   = currentTime - prevTime;

        if (changeTime > 500)
        {
            Hand       hand             = currentFrame.Hands[0];
            FingerList fingers          = hand.Fingers;
            Hand       prevHand         = prevFrame.Hands[0];
            FingerList prevFingers      = prevHand.Fingers;
            Pointable  currentPointable = currentFrame.Pointables[0];
            Pointable  prevPointable    = prevFrame.Pointables[0];
            // Set Up Screen
            Leap.Screen currentScreen = controller.LocatedScreens.ClosestScreenHit(currentPointable); //CalibratedScreens.ClosestScreenHit(currentPointable);
            Leap.Screen prevScreen    = controller.LocatedScreens.ClosestScreenHit(prevPointable);
            // Set up Display
            float  DisplayHeight = currentScreen.HeightPixels;
            float  DisplayWidth  = currentScreen.WidthPixels;
            double scalingFactor = 1.5;
            // Set up Coordinates
            float CurrentLeapXCoordinate = currentScreen.Intersect(currentPointable, true, 1.0F).x;
            float CurrentLeapYCoordinate = currentScreen.Intersect(currentPointable, true, 1.0F).y;
            int   CurrentyPixel          = (int)(DisplayHeight - (DisplayHeight * (scalingFactor * CurrentLeapYCoordinate)));
            int   CurrentxPixel          = (int)(DisplayWidth * (scalingFactor * CurrentLeapXCoordinate));
            float PrevLeapXCoordinate    = currentScreen.Intersect(prevPointable, true, 1.0F).x;
            float PrevLeapYCoordinate    = currentScreen.Intersect(prevPointable, true, 1.0F).y;
            int   PrevyPixel             = (int)(DisplayHeight - (DisplayHeight * (scalingFactor * PrevLeapYCoordinate)));
            int   PrevxPixel             = (int)(DisplayWidth * (scalingFactor * PrevLeapXCoordinate));
            float changeyPixel           = (PrevyPixel - CurrentyPixel);
            float changexPixel           = (PrevxPixel - CurrentxPixel);
            if (changeyPixel < 0)
            {
                changeyPixel = changeyPixel * -1;
            }
            if (changexPixel < 0)
            {
                changexPixel = changexPixel * -1;
            }
            bool allowfalse = false;
            if (changeyPixel > 10)
            {
                allowfalse = true;
            }
            if (CurrentyPixel < 0)
            {
                CurrentyPixel = 0;
            }
            if (CurrentxPixel < 0)
            {
                CurrentxPixel = 0;
            }
            if (allowfalse)
            {
                if (prevFingers.Count != 2 || prevFingers.Count != 3)
                {
                    if (fingers.Count == 1)
                    {
                        if (changeTime > 500)
                        {
                            UnityEngine.Debug.Log("TipPosition: " + fingers[0].TipPosition + " Width: " + CurrentxPixel + " height: " + CurrentyPixel + "\n");
                            SetCursorPos(CurrentxPixel, CurrentyPixel);
                        }
                    }
                    if (fingers.Count == 2)
                    {
                        if (changeTime > 1000)
                        {
                            mouse_event(0x0002 | 0x0004, 0, CurrentxPixel, CurrentyPixel, 0);
                            UnityEngine.Debug.Log("Clicked At " + CurrentxPixel + " " + CurrentyPixel + "\n");
                        }
                    }
                    if (fingers.Count == 4)
                    {
                        if (changeTime > 10000)
                        {
                            float Translation = currentFrame.Translation(prevFrame).y;
                            Console.WriteLine(Translation);
                            if (Translation < -50)
                            {
                                //SendKeys.SendWait("{LEFT}");
                                System.Threading.Thread.Sleep(700);
                            }
                            if (Translation > 50)
                            {
                                //SendKeys.SendWait("{Right}");
                                System.Threading.Thread.Sleep(700);
                            }
                        }
                    }
                }
            }
        }
        prevTime = currentTime;
    }
Esempio n. 5
0
        public override void OnFrame(Controller cntrlr)
        {
            Frame currentFrame = cntrlr.Frame();

            currentTime    = currentFrame.Timestamp;
            timeDifference = currentTime - previousTime;

            if (timeDifference > 1000)
            {
                if (!currentFrame.Hands.IsEmpty)  //van kéz az aktuális Frame-ben



                {
                    Hand   RightHand = currentFrame.Hands.Rightmost;
                    Hand   LeftHand  = currentFrame.Hands.Leftmost;
                    Finger finger    = RightHand.Fingers.Frontmost;

                    Leap.Screen screen = cntrlr.LocatedScreens.ClosestScreenHit(finger);
                    if (screen.IsValid)
                    {
                        var tipVel = (int)finger.TipVelocity.Magnitude;

                        if (tipVel > 25)
                        {
                            var xScreenIntersect = screen.Intersect(finger, true).x;
                            var yScreenIntersect = screen.Intersect(finger, true).y;

                            if (xScreenIntersect.ToString() != "NaN")
                            {
                                int x = (int)(xScreenIntersect * screen.WidthPixels);
                                int y = (int)(screen.HeightPixels - (yScreenIntersect * screen.HeightPixels));
                                if (RightHand.Fingers.Count < 3)
                                {
                                    Cursor.MoveCursor(x, y);
                                }

                                GestureList gestures = currentFrame.Gestures();
                                for (int i = 0; i < gestures.Count; i++)
                                {
                                    Gesture gesture = gestures[i];

                                    switch (gesture.Type)
                                    {
                                    case Gesture.GestureType.TYPECIRCLE:
                                        CircleGesture circle = new CircleGesture(gesture);

                                        String clockwiseness;
                                        if (circle.Pointable.Direction.AngleTo(circle.Normal) <= Math.PI / 4)
                                        {
                                            //Clockwise if angle < 90°
                                            clockwiseness = "clockwise";
                                        }
                                        else
                                        {
                                            clockwiseness = "counterclockwise";
                                        }
                                        if (currentFrame.Hands.Count == 1 && currentFrame.Fingers.Count == 1)
                                        {
                                            if (currentTime - VolTime > TipPause)
                                            {
                                                VolTime = currentTime;
                                                if (clockwiseness.Equals("clockwise"))
                                                {
                                                    Cursor.keybd_event((byte)Keys.VolumeUp, 0, 0, 0);
                                                }
                                                if (clockwiseness.Equals("counterclockwise"))
                                                {
                                                    Cursor.keybd_event((byte)Keys.VolumeDown, 0, 0, 0);
                                                }
                                            }
                                        }
                                        float sweptAngle = 0;


                                        if (circle.State != Gesture.GestureState.STATESTART)
                                        {
                                            CircleGesture previousUpdate = new CircleGesture(cntrlr.Frame(1).Gesture(circle.Id));
                                            sweptAngle = (circle.Progress - previousUpdate.Progress) * 360;
                                        }

                                        Console.WriteLine("Circle id: " + circle.Id
                                                          + ", " + circle.State
                                                          + ", progress: " + circle.Progress
                                                          + ", radius: " + circle.Radius
                                                          + ", angle: " + sweptAngle
                                                          + ", " + clockwiseness);
                                        break;

                                    case Gesture.GestureType.TYPESWIPE:
                                        if (currentFrame.Fingers.Count >= 5 && currentFrame.Hands.Count == 1)
                                        {
                                            if (currentTime - swipeTime > TipPause)
                                            {
                                                swipeTime = currentTime;
                                                SwipeGesture swipe = new SwipeGesture(gesture);
                                                Console.WriteLine("Swipe id: " + swipe.Id
                                                                  + ", " + swipe.State
                                                                  + ", position: " + swipe.Position
                                                                  + ", direction: " + swipe.Direction
                                                                  + ", speed: " + swipe.Speed);

                                                if (swipe.Direction.x < 0)
                                                {
                                                    System.Windows.Forms.SendKeys.SendWait("%{LEFT}");     // alt+leftarrow
                                                    Console.WriteLine("back");
                                                }
                                                if (swipe.Direction.x > 0)
                                                {
                                                    System.Windows.Forms.SendKeys.SendWait("%{RIGHT}");     // alt+rightarrow
                                                    Console.WriteLine("fwd");
                                                }
                                            }
                                        }

                                        break;

                                    case Gesture.GestureType.TYPEKEYTAP:
                                        KeyTapGesture keytap = new KeyTapGesture(gesture);
                                        Console.WriteLine("Tap id: " + keytap.Id
                                                          + ", " + keytap.State
                                                          + ", position: " + keytap.Position
                                                          + ", direction: " + keytap.Direction);
                                        break;

                                    case Gesture.GestureType.TYPESCREENTAP:
                                        ScreenTapGesture screentap = new ScreenTapGesture(gesture);
                                        Console.WriteLine("Tap id: " + screentap.Id
                                                          + ", " + screentap.State
                                                          + ", position: " + screentap.Position
                                                          + ", direction: " + screentap.Direction);
                                        break;

                                    default:
                                        Console.WriteLine("Unknown gesture");
                                        break;
                                    }
                                }
                            }
                        }

                        if (currentFrame.Hands.Count == 2 && LeftHand.Fingers.Count <= 2)
                        {
                            if (Extensions.IsTapping(LeftHand.Fingers.Frontmost))
                            {
                                //Mouse.LeftClick();
                                if ((currentTime - MouseDown) / 2 > TipPause)
                                {
                                    MouseDown = currentTime;
                                    Console.WriteLine("Left " + i);
                                    Cursor.mouse_event(0x02 | 0x04, (uint)x, (uint)y, 0, 0);
                                }
                                i = i + 1;
                                return;
                            }

                            if (Extensions.IsTapping(LeftHand.Fingers.Rightmost))
                            {
                                //Mouse.RightClick();
                                if ((currentTime - MouseDown) / 2 > TipPause)
                                {
                                    MouseDown = currentTime;
                                    Console.WriteLine("Right " + i);
                                    Cursor.mouse_event(0x08 | 0x10, (uint)x, (uint)y, 0, 0);
                                }
                                i = i + 1;
                                return;
                            }
                        }

                        if (currentFrame.Hands.Count == 2 && LeftHand.Fingers.Count >= 4 && RightHand.Fingers.Count <= 2)
                        {
                            //scrl dwn
                            if (LeftHand.Direction.y > 0.5)
                            {
                                Console.WriteLine("scrl up" + i);

                                //Cursor.keybd_event(38, 0, 0, 0); // 40 == up arw key

                                System.Windows.Forms.SendKeys.SendWait("{UP}");

                                i = i + 1;
                            }

                            if (LeftHand.Direction.y < 0)
                            {
                                Console.WriteLine("scrl down " + i);
                                //   Cursor.keybd_event(40, 0, 0, 0); // 38 == dwn arw key

                                System.Windows.Forms.SendKeys.SendWait("{DOWN}");

                                i = i + 1;
                            }
                            return;
                        }

                        if (currentFrame.Hands.Count == 2 && LeftHand.Fingers.Count >= 4 && RightHand.Fingers.Count >= 4)
                        {
                            if (oskIsOpened == false)
                            {
                                Process.Start(@"C:\Windows\System32\osk.exe");
                                oskIsOpened = true;
                            }
                        }
                    }
                }

                previousTime = currentTime;
            }
        }