0

我创建了一个虚拟键盘,弹出 wpf 文本框控件的键盘。单击网页内的文本框时如何使键盘弹出?

显示文本框的逻辑如下:

static void TouchScreenKeyboardPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement host = sender as FrameworkElement;
            if (host != null)
            {
                host.GotFocus += new RoutedEventHandler(OnGotFocus);
                host.LostFocus += new RoutedEventHandler(OnLostFocus);
            }
        }

    static void OnGotFocus(object sender, RoutedEventArgs e)
    {
        Control host = sender as Control;

        if (sender == typeof(TextBox))
        {
            _PreviousTextBoxBackgroundBrush = host.Background;
            _PreviousTextBoxBorderBrush = host.BorderBrush;
            _PreviousTextBoxBorderThickness = host.BorderThickness;

            host.Background = Brushes.Yellow;
            host.BorderBrush = Brushes.Red;
            host.BorderThickness = new Thickness(4);
        }


        _CurrentControl = host;

        if (_InstanceObject == null)
        {
            FrameworkElement ct = host;
            while (true)
            {
                if (ct is Window)
                {
                    ((Window)ct).LocationChanged += new EventHandler(TouchScreenKeyboard_LocationChanged);
                    ((Window)ct).Activated += new EventHandler(TouchScreenKeyboard_Activated);
                    ((Window)ct).Deactivated += new EventHandler(TouchScreenKeyboard_Deactivated);
                    break;
                }
                if(ct.Parent != null)
                ct = (FrameworkElement)ct.Parent;
            }

            _InstanceObject = new TouchScreenKeyboard();
            _InstanceObject.AllowsTransparency = true;
            _InstanceObject.WindowStyle = WindowStyle.None;
            _InstanceObject.ShowInTaskbar = false;
            _InstanceObject.ShowInTaskbar = false;
            _InstanceObject.Topmost = true;

           host.LayoutUpdated += new EventHandler(tb_LayoutUpdated);
        }
    }

    static void TouchScreenKeyboard_Deactivated(object sender, EventArgs e)
    {
        if (_InstanceObject != null)
        {
            _InstanceObject.Topmost = false;
        }
    }

    static void TouchScreenKeyboard_Activated(object sender, EventArgs e)
    {
        if (_InstanceObject != null)
        {
            _InstanceObject.Topmost = true;
        }
    }

    static void TouchScreenKeyboard_LocationChanged(object sender, EventArgs e)
    {
        syncchild();
    }

    static void tb_LayoutUpdated(object sender, EventArgs e)
    {
        syncchild();
    }

    static void OnLostFocus(object sender, RoutedEventArgs e)
    {

        Control host = sender as Control;
        host.Background = _PreviousTextBoxBackgroundBrush;
        host.BorderBrush = _PreviousTextBoxBorderBrush;
        host.BorderThickness = _PreviousTextBoxBorderThickness;

        if (_InstanceObject != null)
        {
            _InstanceObject.Close();
            _InstanceObject = null;
        }
    }

    #endregion
}

    private static void syncchild()
    {
        if (_CurrentControl != null && _InstanceObject != null)
        {

            Point virtualpoint = new Point(0, _CurrentControl.ActualHeight + 3);
            Point Actualpoint = _CurrentControl.PointToScreen(virtualpoint);

            if (WidthTouchKeyboard + Actualpoint.X > SystemParameters.VirtualScreenWidth)
            {
                double difference = WidthTouchKeyboard + Actualpoint.X - SystemParameters.VirtualScreenWidth;
                _InstanceObject.Left = Actualpoint.X - difference;
            }
            else if (!(Actualpoint.X > 1))
            {
                _InstanceObject.Left = 1;
            }
            else
                _InstanceObject.Left = Actualpoint.X;

            _InstanceObject.Top = Actualpoint.Y;
            _InstanceObject.Show();
        }
    }

 private static void SetKeyInBrowser(string key)
    {
        var elementName = (((mshtml.HTMLDocument)(dom)).activeElement).id;

        if (elementName != null)
        {
            var existingText = dom.getElementById(elementName).getAttribute("value");
            if (existingText == null)
            {
                existingText = "";
            }
            //if it's a backspace.
            if (isBackspace)
            {
                existingText = existingText.ToString().Remove(existingText.ToString().Length - 1);
                dom.getElementById(elementName).setAttribute("value", existingText.ToString());
            }
            else if (key.Length != 0)
            {
                dom.getElementById(elementName).setAttribute("value", existingText.ToString() + key[key.Length - 1]);
            }
        }
        isBackspace = false;
    }
4

1 回答 1

1

您需要为此使用javascript。您需要知道该文本字段何时像这样聚焦: 检查焦点是否在文本字段上

然后,一旦在 JavaScript 上触发了焦点事件,您应该调用 WPF 函数来显示键盘。以下是有用的提示:

如何以编程方式检查在 WPF 浏览器中加载的网页内的文本输入框是否具有焦点?

于 2012-08-15T08:39:18.600 回答