WdFullKeyboard.xaml.cs 8.82 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace FLY.ControlLibrary.UI.OSK
{
    /// <summary>
    /// WdFullKeyboard.xaml 的交互逻辑
    /// </summary>
    /// <summary>
    /// WdFullKeyboard.xaml 的交互逻辑
    /// </summary>
    /// <summary>
    /// Window_FullPad.xaml 的交互逻辑
    /// </summary>
    public partial class WdFullKeyboard : Window, INotifyPropertyChanged, IVirtualKeyboard
    {
        private DispatcherTimer timer;
        private TextBox textbox = null;
        private bool press1st = true;
        public bool IsPwMode { get; set; } = false;
        FullKeyboardViewModel keyboard;
        /// <summary>
        /// 全键盘
        /// </summary>
        public WdFullKeyboard()
        {
            InitializeComponent();
            keyboard = new FullKeyboardViewModel();
            timer = new DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = TimeSpan.FromMilliseconds(1);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            this.ShowDialog();

        }
        public void Open(TextBox textbox)
        {

            this.textbox = textbox;
            this.Result = textbox.Text;

            timer.Start();//不能直接showdialog(), 要让事件继续传递,直到有个框出来!!!

            //this.ShowDialog();
        }

        void updateResultView()
        {
            if (!IsPwMode)
            {
                this.ResultView = this.Result;
            }
            else
            {
                string star = "";
                for (int i = 0; i < this.Result.Length; i++)
                {
                    star += "*";
                }
                this.ResultView = star;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = keyboard;
            this.textblock_result.DataContext = this;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {

            if (press1st)
            {
                press1st = false;
                Result = "";
            }

            string text = ((sender as Button).Content as TextBlock).Text;

            Result += text;
        }

        private void button_caps_Click(object sender, RoutedEventArgs e)
        {
            keyboard.IsCapsLock = !keyboard.IsCapsLock;
        }

        private void button_backspace_Click(object sender, RoutedEventArgs e)
        {
            Result = "";
        }

        private void button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button_123_Click(object sender, RoutedEventArgs e)
        {
            keyboard.Is123 = !keyboard.Is123;
        }

        private void button_space_Click(object sender, RoutedEventArgs e)
        {
            Result += " ";
        }

        private void button_enter_Click(object sender, RoutedEventArgs e)
        {
            if (this.textbox != null)
            {
                this.textbox.Text = Result;
                //触发失去焦点事件!!!!!!!
                //让 绑定的 property 生效
                this.textbox.RaiseEvent(new RoutedEventArgs(TextBox.LostFocusEvent));
            }
            this.Close();
        }


        private string result;
        /// <summary>
        /// 结果
        /// </summary>
        public string Result
        {
            get { return result; }
            set
            {
                if (result != value)
                {
                    result = value;
                    updateResultView();
                }
            }
        }
        /// <summary>
        /// 正常模式时, ResultView 与 Result 一样, 密码模式时, ResultView 显示*******
        /// </summary>
        public string ResultView { get; set; }

        /// <summary>
        /// 属性改变事件
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void button_dot_Click(object sender, RoutedEventArgs e)
        {
            Result += keyboard.KeyDot;
        }


    }
    public class FullKeyboardViewModel : INotifyPropertyChanged
    {

        public string KeyQ { get; set; }


        public string KeyW { get; set; }

        public string KeyE { get; set; }


        public string KeyR { get; set; }

        public string KeyT { get; set; }

        public string KeyY { get; set; }

        public string KeyU { get; set; }

        public string KeyI { get; set; }

        public string KeyO { get; set; }

        public string KeyP { get; set; }

        public string KeyA { get; set; }

        public string KeyS { get; set; }

        public string KeyD { get; set; }

        public string KeyF { get; set; }

        public string KeyG { get; set; }

        public string KeyH { get; set; }

        public string KeyJ { get; set; }

        public string KeyK { get; set; }

        public string KeyL { get; set; }

        public string KeyZ { get; set; }

        public string KeyX { get; set; }

        public string KeyC { get; set; }

        public string KeyV { get; set; }

        public string KeyB { get; set; }

        public string KeyN { get; set; }

        public string KeyM { get; set; }


        public bool IsCapsLock { get; set; }

        public bool Is123 { get; set; }

        public string KeyDot { get; set; }



        public FullKeyboardViewModel()
        {
            IsCapsLock = false;
            Is123 = false;

            UpdateKey();

            this.PropertyChanged += new PropertyChangedEventHandler(FullKeyPad_PropertyChanged);
        }

        void FullKeyPad_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Is123")
            {
                UpdateKey();
            }
            if (e.PropertyName == "IsCapsLock")
            {
                UpdateKey();
            }
        }
        void UpdateKey()
        {
            if (Is123)
            {
                To123();
            }
            else
            {
                if (IsCapsLock)
                    ToUpper();
                else
                    ToLower();
            }
        }
        void To123()
        {
            KeyQ = "1";
            KeyW = "2";
            KeyE = "3";
            KeyR = "4";
            KeyT = "5";
            KeyY = "6";
            KeyU = "7";
            KeyI = "8";
            KeyO = "9";
            KeyP = "0";
            KeyA = "!";
            KeyS = "@";
            KeyD = "#";
            KeyF = "$";
            KeyG = "%";
            KeyH = "&";
            KeyJ = "*";
            KeyK = "(";
            KeyL = ")";
            KeyZ = "'";
            KeyX = "/";
            KeyC = "-";
            KeyV = "_";
            KeyB = ":";
            KeyN = ";";
            KeyM = "?";
            KeyDot = ".";
        }
        void ToUpper()
        {
            KeyQ = "Q";
            KeyW = "W";
            KeyE = "E";
            KeyR = "R";
            KeyT = "T";
            KeyY = "Y";
            KeyU = "U";
            KeyI = "I";
            KeyO = "O";
            KeyP = "P";
            KeyA = "A";
            KeyS = "S";
            KeyD = "D";
            KeyF = "F";
            KeyG = "G";
            KeyH = "H";
            KeyJ = "J";
            KeyK = "K";
            KeyL = "L";
            KeyZ = "Z";
            KeyX = "X";
            KeyC = "C";
            KeyV = "V";
            KeyB = "B";
            KeyN = "N";
            KeyM = "M";
            KeyDot = ",";
        }
        void ToLower()
        {
            KeyQ = "q";
            KeyW = "w";
            KeyE = "e";
            KeyR = "r";
            KeyT = "t";
            KeyY = "y";
            KeyU = "u";
            KeyI = "i";
            KeyO = "o";
            KeyP = "p";
            KeyA = "a";
            KeyS = "s";
            KeyD = "d";
            KeyF = "f";
            KeyG = "g";
            KeyH = "h";
            KeyJ = "j";
            KeyK = "k";
            KeyL = "l";
            KeyZ = "z";
            KeyX = "x";
            KeyC = "c";
            KeyV = "v";
            KeyB = "b";
            KeyN = "n";
            KeyM = "m";
            KeyDot = ",";
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

}