KeyboardBehavior.cs 5.7 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows;
using System.Windows.Controls;

namespace FLY.UI.OSK
{
    /// <summary>
    /// 为textbox 添加 虚拟键盘 
    /// </summary>
    public class KeyboardBehavior : Behavior<UIElement>
    {
        /// <summary>
        /// 使能 点击 textbox 弹出虚拟键盘
        /// </summary>
        public static bool Enable = true;
        /// <summary>
        /// 使能 鼠标点击 textbox 弹出虚拟键盘, 只是为了测试!!!!
        /// </summary>
        public static bool EnableMouseDown = false;
        /// <summary>
        /// 虚拟键盘正在显示
        /// </summary>
        public static bool IsKeyboardOnShow = false;

        public KeyboardBehavior()
        {

        }
        /// <summary>
        /// 创建控件时调用
        /// </summary>
        protected override void OnAttached()
        {

            base.OnAttached();
            DependencyObject dobj = AssociatedObject;//它是Border
            do
            {
                if (dobj is System.Windows.Controls.TextBox)
                {
                    break;
                }
                dobj = System.Windows.Media.VisualTreeHelper.GetParent(dobj);
            } while (dobj != null);

            TextBox tb = dobj as TextBox;

            if (!tb.AcceptsReturn) //它不能通过按 Enter 键触发换行。 使按Enter 能实现LostFocus
            {
                tb.KeyDown += AssociatedObject_KeyDown;
            }
            //不能在 textbox 添加点击事件, 它根本不触发。。。
            //tb.TouchDown += AssociatedObject_TouchDown;
            //tb.MouseDown += AssociatedObject_MouseDown;


            AssociatedObject.TouchUp += AssociatedObject_TouchDown;
            AssociatedObject.MouseDown += AssociatedObject_MouseDown;
        }

        void AssociatedObject_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                TextBox tb = sender as TextBox;
                if (tb.AcceptsReturn)
                    return;
                //触发失去焦点事件!!!!!!!
                //让 绑定的 property 生效

                tb.RaiseEvent(new RoutedEventArgs(TextBox.LostFocusEvent));
            }
        }
        /// <summary>
        /// 销毁控件时调用
        /// </summary>
        protected override void OnDetaching()
        {
            DependencyObject dobj = AssociatedObject;
            do
            {
                if (dobj is System.Windows.Controls.TextBox)
                {
                    break;
                }
                dobj = System.Windows.Media.VisualTreeHelper.GetParent(dobj);
            } while (dobj != null);

            TextBox tb = dobj as TextBox;
            tb.KeyDown -= AssociatedObject_KeyDown;

            //border
            AssociatedObject.TouchDown -= AssociatedObject_TouchDown;
            AssociatedObject.MouseDown -= AssociatedObject_MouseDown;

            base.OnDetaching();
        }


        void AssociatedObject_TouchDown(object sender, System.Windows.Input.TouchEventArgs e)
        {
            if (IsKeyboardOnShow)//虚拟键盘已经打开
                return;

            //press release 都会调用
            if (!Enable)
                return;
            DependencyObject dobj = sender as DependencyObject;
            popPad(dobj);

            //不能在 press 时 handle = true。 这样会导致焦点没法落在 textbox 上,而且也没有了 release
            //e.Handled = true;

        }

        void AssociatedObject_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (IsKeyboardOnShow)//虚拟键盘已经打开
                return;

            //press release 都会调用
            if (e.ButtonState == System.Windows.Input.MouseButtonState.Released)
                return;

            if (!Enable)
                return;
            if (!EnableMouseDown)
                return;

            DependencyObject dobj = sender as DependencyObject;
            popPad(dobj);
            //不能在 press 时 handle = true。 这样会导致焦点没法落在 textbox 上,而且也没有了 release
            //e.Handled = true;
        }

        void popPad(DependencyObject dobj)
        {
            //向上查找,直到是System.Windows.Controls.TextBox
            do
            {
                if (dobj is System.Windows.Controls.TextBox)
                {
                    break;
                }
                dobj = System.Windows.Media.VisualTreeHelper.GetParent(dobj);
            } while (dobj != null);


            DependencyObject dobj_window = dobj;
            //一直向上找,直到找到window
            do
            {
                if (dobj_window is Window)
                {
                    break;
                }
                dobj_window = System.Windows.Media.VisualTreeHelper.GetParent(dobj_window);
            } while (dobj_window != null);


            if (dobj != null)
            {
                System.Windows.Controls.TextBox tb = dobj as System.Windows.Controls.TextBox;
                IVirtualKeyboard kb;
                if ((tb.Tag is string) && ((string)tb.Tag == "Full"))
                    kb = new Window_FullPad();
                else
                    kb = new Window_NumPad();

                Window w = (Window)kb;
                w.Owner = dobj_window as Window;
                IsKeyboardOnShow = true;
                w.Closed += (s, e) => { IsKeyboardOnShow = false; };

                kb.Open(tb);
            }
        }

    }

}