Пример #1
0
        public bool OnKeyPressed(
            Key key,
            KeyState key_state,
            float speed_up,
            bool is_repetition,
            bool is_modifier,
            InputProvider input_provider)
        {
            // Cntrl+click, alt+click should work.
            if (key == Key.Unbound && is_modifier)
            {
                return(false);
            }
            if (key == Key.Modifier)
            {
                if (key_state == KeyState.Down)
                {
                    helper_window.Hide();
                    eye_tracking_mouse.StartControlling();
                    return(true);
                }

                if (key_state == KeyState.Up)
                {
                    if (eye_tracking_mouse.mouse_state == EyeTrackingMouse.MouseState.Controlling)
                    {
                        starting_cursor_position = System.Windows.Forms.Cursor.Position;
                        eye_tracking_mouse.StartCalibration(false);
                        helper_window.Show();
                        return(true);
                    }

                    return(false);
                }
            }

            if (eye_tracking_mouse.mouse_state != EyeTrackingMouse.MouseState.Calibrating)
            {
                if (eye_tracking_mouse.mouse_state == EyeTrackingMouse.MouseState.Controlling)
                {
                    helper_window.Hide();
                    eye_tracking_mouse.StopControlling();
                    input_provider.SendModifierDown();
                }
                return(false);
            }

            if (key == Key.StopCalibration)
            {
                if (key_state == KeyState.Up)
                {
                    helper_window.Hide();
                    eye_tracking_mouse.StopControlling();
                }
                return(true);
            }

            if (key == Key.Unbound)
            {
                // Unbound key in Accessibility mode might mean that User has problems with pressing the intended buttons.
                return(true);
            }

            var repetition_white_list = new SortedSet <Key> {
                Key.ScrollDown,
                Key.ScrollUp,
                Key.ScrollLeft,
                Key.ScrollRight,
                Key.CalibrateLeft,
                Key.CalibrateRight,
                Key.CalibrateUp,
                Key.CalibrateDown,
            };

            if (is_repetition && !repetition_white_list.Contains(key))
            {
                return(true);
            }

            if (key_state == KeyState.Down)
            {
                // Calibration
                int calibration_step = (int)(Options.Instance.calibration_step * speed_up);
                if (key == Key.CalibrateLeft)
                {
                    eye_tracking_mouse.AdjustCursorPosition(-calibration_step, 0);
                }
                if (key == Key.CalibrateRight)
                {
                    eye_tracking_mouse.AdjustCursorPosition(calibration_step, 0);
                }
                if (key == Key.CalibrateUp)
                {
                    eye_tracking_mouse.AdjustCursorPosition(0, -calibration_step);
                }
                if (key == Key.CalibrateDown)
                {
                    eye_tracking_mouse.AdjustCursorPosition(0, +calibration_step);
                }
                // Scroll
                if (key == Key.ScrollDown)
                {
                    MouseButtons.WheelDown((int)(Options.Instance.vertical_scroll_step * speed_up));
                }
                if (key == Key.ScrollUp)
                {
                    MouseButtons.WheelUp((int)(Options.Instance.vertical_scroll_step * speed_up));
                }
                if (key == Key.ScrollLeft)
                {
                    MouseButtons.WheelLeft((int)(Options.Instance.horizontal_scroll_step * speed_up));
                }
                if (key == Key.ScrollRight)
                {
                    MouseButtons.WheelRight((int)(Options.Instance.horizontal_scroll_step * speed_up));
                }
            }

            // Mouse buttons
            if (eye_tracking_mouse.mouse_state == EyeTrackingMouse.MouseState.Calibrating &&
                (key == Key.Accessibility_SaveCalibration))
            {
                if (key_state == KeyState.Up)
                {
                    helper_window.HideCalibration();
                    eye_tracking_mouse.SaveCalibration();
                }
                else
                {
                    helper_window.ShowCalibration(starting_cursor_position);
                }
            }

            if (key == Key.LeftMouseButton)
            {
                if (key_state == KeyState.Down)
                {
                    eye_tracking_mouse.LeftDown();
                }
                else if (key_state == KeyState.Up)
                {
                    eye_tracking_mouse.LeftUp();
                }
            }

            if (key == Key.RightMouseButton)
            {
                if (key_state == KeyState.Down)
                {
                    eye_tracking_mouse.RightDown();
                }
                else if (key_state == KeyState.Up)
                {
                    eye_tracking_mouse.RightUp();
                }
            }

            if (key == Key.ShowCalibrationView && key_state == KeyState.Down)
            {
                CalibrationManager.Instance.IsDebugWindowEnabled = !CalibrationManager.Instance.IsDebugWindowEnabled;
            }

            return(true);
        }
        public bool OnKeyPressed(
            Key key,
            KeyState key_state,
            float speed_up,
            bool is_repetition,
            bool is_modifier,
            InputProvider input_provider)
        {
            // The application grabs control over cursor when modifier is pressed.
            if (key == Key.Modifier)
            {
                if (key_state == KeyState.Down)
                {
                    eye_tracking_mouse.StartControlling();
                    return(true);
                }

                if (key_state == KeyState.Up)
                {
                    if (eye_tracking_mouse.mouse_state == EyeTrackingMouse.MouseState.Idle)
                    {
                        return(false);
                    }

                    eye_tracking_mouse.StopControlling();
                    return(true);
                }
            }

            if (eye_tracking_mouse.mouse_state == EyeTrackingMouse.MouseState.Idle)
            {
                return(false);
            }

            if (key == Key.StopCalibration && always_on)
            {
                if (key_state == KeyState.Up)
                {
                    eye_tracking_mouse.StopControlling();
                }
                return(true);
            }

            if (key == Key.Unbound || key == Key.StopCalibration || key == Key.Accessibility_SaveCalibration)
            {
                // The application intercepts modifier key presses. We do not want to lose modifier when handling unbound keys.
                // We stop controlling cursor when facing the first unbound key and send modifier keystroke to OS before handling pressed key.
                // This way key combinations like 'Win+E' remain available.
                if (!is_modifier)
                {
                    if (!always_on)
                    {
                        input_provider.SendModifierDown();
                    }
                    eye_tracking_mouse.StopControlling();
                }
                return(false);
            }

            var repetition_white_list = new SortedSet <Key> {
                Key.ScrollDown,
                Key.ScrollUp,
                Key.ScrollLeft,
                Key.ScrollRight,
                Key.CalibrateLeft,
                Key.CalibrateRight,
                Key.CalibrateUp,
                Key.CalibrateDown,
            };

            if (is_repetition && !repetition_white_list.Contains(key))
            {
                return(true);
            }

            if (key_state == KeyState.Down)
            {
                // Calibration
                int calibration_step = (int)(Options.Instance.calibration_step * speed_up);
                if (key == Key.CalibrateLeft)
                {
                    eye_tracking_mouse.StartCalibration(true);
                    eye_tracking_mouse.AdjustCursorPosition(-calibration_step, 0);
                }
                if (key == Key.CalibrateRight)
                {
                    eye_tracking_mouse.StartCalibration(true);
                    eye_tracking_mouse.AdjustCursorPosition(calibration_step, 0);
                }
                if (key == Key.CalibrateUp)
                {
                    eye_tracking_mouse.StartCalibration(true);
                    eye_tracking_mouse.AdjustCursorPosition(0, -calibration_step);
                }
                if (key == Key.CalibrateDown)
                {
                    eye_tracking_mouse.StartCalibration(true);
                    eye_tracking_mouse.AdjustCursorPosition(0, +calibration_step);
                }

                // Scroll
                if (key == Key.ScrollDown)
                {
                    MouseButtons.WheelDown((int)(Options.Instance.vertical_scroll_step * speed_up));
                }
                if (key == Key.ScrollUp)
                {
                    MouseButtons.WheelUp((int)(Options.Instance.vertical_scroll_step * speed_up));
                }
                if (key == Key.ScrollLeft)
                {
                    MouseButtons.WheelLeft((int)(Options.Instance.horizontal_scroll_step * speed_up));
                }
                if (key == Key.ScrollRight)
                {
                    MouseButtons.WheelRight((int)(Options.Instance.horizontal_scroll_step * speed_up));
                }
            }

            // Mouse buttons
            if (eye_tracking_mouse.mouse_state == EyeTrackingMouse.MouseState.Calibrating &&
                (key == Key.LeftMouseButton || key == Key.RightMouseButton))
            {
                eye_tracking_mouse.SaveCalibration();
                eye_tracking_mouse.StartControlling();
            }

            if (key == Key.LeftMouseButton)
            {
                if (key_state == KeyState.Down)
                {
                    eye_tracking_mouse.LeftDown();
                }
                else if (key_state == KeyState.Up)
                {
                    eye_tracking_mouse.LeftUp();
                }
            }

            if (key == Key.RightMouseButton)
            {
                if (key_state == KeyState.Down)
                {
                    eye_tracking_mouse.RightDown();
                }
                else if (key_state == KeyState.Up)
                {
                    eye_tracking_mouse.RightUp();
                }
            }

            if (key == Key.ShowCalibrationView && key_state == KeyState.Down)
            {
                CalibrationManager.Instance.IsDebugWindowEnabled = !CalibrationManager.Instance.IsDebugWindowEnabled;
            }

            return(true);
        }