wenxiangchao
4 天以前 2e889843b4c171f2d6ad8e0d53084040703d11f6
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
using Panuon.WPF.UI;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
 
namespace HMI.UI
{
    public partial class KeyboardWindow : WindowX
    {
        private bool _isCapsLockOn = false;
 
        public bool IsCapsLockOn
        {
            get => _isCapsLockOn;
            set
            {
                Set(ref _isCapsLockOn, value);
            }
        }
 
        public KeyboardWindow()
        {
            DataContext = this;
            InitializeComponent();
        }
 
        public bool? Open(string data)
        {
            Data = data;
            inputTextBox.Text = data;
            return this.ShowDialog();
        }
 
        public string Data { get; private set; }
 
        private void KeyButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button)
            {
                string key = button.Content.ToString();
                if (_isCapsLockOn && IsLetter(key))
                {
                    key = key.ToUpper();
                }
                else
                {
                    key = key.ToLower();
                }
                InsertText(key);
            }
        }
 
        private bool IsLetter(string key)
        {
            return key.Length == 1 && char.IsLetter(key[0]);
        }
 
        private void InsertText(string text)
        {
            inputTextBox.Text += text;
            inputTextBox.Focus();
        }
 
        private void BackspaceButton_Click(object sender, RoutedEventArgs e)
        {
            if (inputTextBox.Text.Length > 0)
            {
                inputTextBox.Text = inputTextBox.Text.Substring(0, inputTextBox.Text.Length - 1);
            }
            inputTextBox.Focus();
        }
 
        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            inputTextBox.Clear();
            inputTextBox.Focus();
        }
 
        private void EnterButton_Click(object sender, RoutedEventArgs e)
        {
            //InsertText(Environment.NewLine);
        }
 
        private void SpaceButton_Click(object sender, RoutedEventArgs e)
        {
            InsertText(" ");
        }
 
        private void CapsButton_Click(object sender, RoutedEventArgs e)
        {
            IsCapsLockOn = !IsCapsLockOn;
        }
 
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            Data = string.Empty;
            DialogResult = false;
            this.Close();
        }
 
        private void Ok_Click(object sender, RoutedEventArgs e)
        {
            Data = inputTextBox.Text;
            DialogResult = true;
            this.Close();
        }
    }
}