private bool IsKeyboardInDockedMode()
        {
            var position = TouchKeyboardHelper.GetTouchKeyboardPosition();

            if (position == null)
            {
                return(false);
            }

            var screenBounds = Screen.PrimaryScreen.Bounds;

            var keyboardPosition = (Rectangle)position;

            if (keyboardPosition.Left <= keyboardDockedPositionMaxDiff &&
                keyboardPosition.Bottom + keyboardDockedPositionMaxDiff >= screenBounds.Height &&
                keyboardPosition.Right + keyboardDockedPositionMaxDiff >= screenBounds.Width)
            {
                return(true);
            }

            return(false);
        }
        private async void keyboardStateCheckTimer_Tick(object sender, EventArgs e)
        {
            var isOpen = TouchKeyboardHelper.IsOpen();

            if (isOpen && !isVisible && IsKeyboardInDockedMode())
            {
                keyboardPosition = TouchKeyboardHelper.GetTouchKeyboardPosition();
                if (keyboardPosition != null)
                {
                    KeyboardOpened((Rectangle)keyboardPosition);
                }
            }
            else if (isOpen && isVisible)
            {
                // Make sure keyboard type is not changed from docked.
                if (!IsKeyboardInDockedMode())
                {
                    KeyboardClosed();
                }
                else
                {
                    // Check for keyboard resize
                    var currentKeyboardPosition = TouchKeyboardHelper.GetTouchKeyboardPosition();

                    if (currentKeyboardPosition != null && keyboardPosition != null &&
                        MuchDifference((Rectangle)currentKeyboardPosition, (Rectangle)keyboardPosition, keyboardDockedPositionMaxDiff))
                    {
                        Debug.WriteLine("Keyboard height changed");
                        UpdateKeyboardHeight((Rectangle)currentKeyboardPosition);
                        keyboardPosition = currentKeyboardPosition;
                    }


                    // Check for foreground app change
                    this.Invoke(new MethodInvoker(() =>
                    {
                        try
                        {
                            if (!TabletModeHelper.IsTabletMode && currentKeyboardPosition != null)
                            {
                                var currentForegroundWindow = WindowManipulationHelper.GetForegroundWindow();
                                if (currentForegroundWindow != lastWindowState.windowHandle)
                                {
                                    Debug.WriteLine("Foreground window changed");
                                    ExpandForegroundWindow();
                                    ShrinkForegroundWindow((Rectangle)currentKeyboardPosition);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Exception in Check for foreground app change: " + ex.ToString());
                        }
                    }));
                }
            }
            else if (!isOpen && isVisible)
            {
                KeyboardClosed();
            }
        }