8 changed files with 801 additions and 129 deletions
@ -1,4 +1,4 @@ |
|||
<Window x:Class="DyeingComputer.Windows.TouchKeyboard" |
|||
<Window x:Class="DyeingComputer.KEY.TouchKeyboard" |
|||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
|||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
|||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
@ -0,0 +1,214 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
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; |
|||
|
|||
namespace DyeingComputer.KEY |
|||
{ |
|||
/// <summary>
|
|||
/// TouchKeyboard.xaml 的交互逻辑
|
|||
/// </summary>
|
|||
public partial class TouchKeyboardNumeral : Window |
|||
{ |
|||
private bool isShiftActive = false; |
|||
private bool isSymbolActive = false; |
|||
public string KeyValue; |
|||
public event EventHandler<KeyPressedEventArgs> KeyPressed; |
|||
private KeyboardMode currentMode = KeyboardMode.Default; |
|||
private int minValue = 0; |
|||
private int maxValue = 9; |
|||
private TextBox targetTextBox; |
|||
// 定义键盘模式枚举
|
|||
public enum KeyboardMode |
|||
{ |
|||
Default, // 默认模式,所有键可用
|
|||
NumericOnly, // 只允许数字
|
|||
DecimalOnly, // 允许数字和小数点
|
|||
LimitedRange // 限制数字范围
|
|||
} |
|||
|
|||
public TouchKeyboardNumeral() |
|||
{ |
|||
InitializeComponent(); |
|||
} |
|||
public void SetKeyboardMode(KeyboardMode mode, TextBox target = null, int min = 0, int max = 9) |
|||
{ |
|||
currentMode = mode; |
|||
targetTextBox = target; |
|||
minValue = min; |
|||
maxValue = max; |
|||
|
|||
UpdateKeyboardLayout(); |
|||
} |
|||
private void UpdateKeyboardLayout() |
|||
{ |
|||
switch (currentMode) |
|||
{ |
|||
case KeyboardMode.NumericOnly: |
|||
// 只允许数字,禁用小数点
|
|||
SetButtonEnabled(".", false); |
|||
break; |
|||
|
|||
case KeyboardMode.DecimalOnly: |
|||
// 允许数字和小数点
|
|||
SetButtonEnabled(".", true); |
|||
break; |
|||
|
|||
case KeyboardMode.LimitedRange: |
|||
// 根据范围启用/禁用数字键
|
|||
for (int i = 0; i <= 9; i++) |
|||
{ |
|||
SetButtonEnabled(i.ToString(), i >= minValue && i <= maxValue); |
|||
} |
|||
break; |
|||
|
|||
case KeyboardMode.Default: |
|||
default: |
|||
// 启用所有键
|
|||
foreach (var element in FirstRow.Children) |
|||
{ |
|||
if (element is Button button && char.IsDigit(button.Content.ToString()[0])) |
|||
button.IsEnabled = true; |
|||
} |
|||
foreach (var element in SecondRow.Children) |
|||
{ |
|||
if (element is Button button && (char.IsDigit(button.Content.ToString()[0]) || button.Content.ToString() == ".")) |
|||
button.IsEnabled = true; |
|||
} |
|||
foreach (var element in ThirdRow.Children) |
|||
{ |
|||
if (element is Button button && char.IsDigit(button.Content.ToString()[0])) |
|||
button.IsEnabled = true; |
|||
} |
|||
break; |
|||
} |
|||
} |
|||
|
|||
// 设置按钮启用状态
|
|||
private void SetButtonEnabled(string content, bool enabled) |
|||
{ |
|||
// 在第一行查找
|
|||
foreach (var element in FirstRow.Children) |
|||
{ |
|||
if (element is Button button && button.Content.ToString() == content) |
|||
{ |
|||
button.IsEnabled = enabled; |
|||
return; |
|||
} |
|||
} |
|||
|
|||
// 在第二行查找
|
|||
foreach (var element in SecondRow.Children) |
|||
{ |
|||
if (element is Button button && button.Content.ToString() == content) |
|||
{ |
|||
button.IsEnabled = enabled; |
|||
return; |
|||
} |
|||
} |
|||
|
|||
// 在第三行查找
|
|||
foreach (var element in ThirdRow.Children) |
|||
{ |
|||
if (element is Button button && button.Content.ToString() == content) |
|||
{ |
|||
button.IsEnabled = enabled; |
|||
return; |
|||
} |
|||
} |
|||
} |
|||
|
|||
// 数字键点击事件
|
|||
private void KeyButton_Click(object sender, RoutedEventArgs e) |
|||
{ |
|||
Button button = sender as Button; |
|||
if (button != null && button.IsEnabled) |
|||
{ |
|||
string keyValue = button.Content.ToString(); |
|||
PreviewTextBox.Text += keyValue; |
|||
|
|||
// 触发按键事件
|
|||
// KeyPressed?.Invoke(this, new KeyPressedEventArgs(KeyType.Character, keyValue));
|
|||
} |
|||
} |
|||
|
|||
// 退格键点击事件
|
|||
private void Backspace_Click(object sender, RoutedEventArgs e) |
|||
{ |
|||
if (PreviewTextBox.Text.Length > 0) |
|||
{ |
|||
PreviewTextBox.Text = PreviewTextBox.Text.Substring(0, PreviewTextBox.Text.Length - 1); |
|||
|
|||
// 触发退格事件
|
|||
// KeyPressed?.Invoke(this, new KeyPressedEventArgs(KeyType.Backspace, ""));
|
|||
} |
|||
} |
|||
|
|||
// 回车键点击事件
|
|||
private void Enter_Click(object sender, RoutedEventArgs e) |
|||
{ |
|||
// 触发回车事件
|
|||
KeyPressed?.Invoke(this, new KeyPressedEventArgs(KeyType.Enter, PreviewTextBox.Text)); |
|||
|
|||
// 隐藏键盘
|
|||
this.Hide(); |
|||
} |
|||
|
|||
// 隐藏键点击事件
|
|||
private void Hide_Click(object sender, RoutedEventArgs e) |
|||
{ |
|||
// KeyPressed?.Invoke(this, new KeyPressedEventArgs(KeyType.Hide, ""));
|
|||
// 隐藏键盘
|
|||
this.Hide(); |
|||
} |
|||
|
|||
public class KeyPressedEventArgs : EventArgs |
|||
{ |
|||
public KeyType KeyType { get; } |
|||
public string KeyValue { get; } |
|||
|
|||
public KeyPressedEventArgs(KeyType keyType, string keyValue) |
|||
{ |
|||
KeyType = keyType; |
|||
KeyValue = keyValue; |
|||
} |
|||
} |
|||
private void Window_Loaded(object sender, RoutedEventArgs e) |
|||
{ |
|||
// 初始化为默认模式
|
|||
SetKeyboardMode(KeyboardMode.Default); |
|||
PreviewTextBox.Text = null; |
|||
} |
|||
} |
|||
// 按键类型枚举
|
|||
public enum KeyType |
|||
{ |
|||
Character, |
|||
Backspace, |
|||
Enter, |
|||
Hide |
|||
} |
|||
|
|||
// 按键事件参数
|
|||
public class KeyPressedEventArgs : EventArgs |
|||
{ |
|||
public KeyType KeyType { get; } |
|||
public string KeyValue { get; } |
|||
public string DATValue { set; get; } |
|||
|
|||
public KeyPressedEventArgs(KeyType keyType, string keyValue) |
|||
{ |
|||
KeyType = keyType; |
|||
KeyValue = keyValue; |
|||
} |
|||
} |
|||
} |
@ -1,68 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
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; |
|||
|
|||
namespace DyeingComputer.Windows |
|||
{ |
|||
/// <summary>
|
|||
/// TouchKeyboard.xaml 的交互逻辑
|
|||
/// </summary>
|
|||
public partial class TouchKeyboardNumeral : Window |
|||
{ |
|||
private bool isShiftActive = false; |
|||
private bool isSymbolActive = false; |
|||
public string KeyValue; |
|||
|
|||
public TouchKeyboardNumeral() |
|||
{ |
|||
InitializeComponent(); |
|||
} |
|||
|
|||
private void KeyButton_Click(object sender, RoutedEventArgs e) |
|||
{ |
|||
Button button = sender as Button; |
|||
if (button != null) |
|||
{ |
|||
string key = button.Content.ToString(); |
|||
|
|||
if (key.Length == 1 && char.IsLetter(key[0])) |
|||
{ |
|||
key = isShiftActive ? key.ToUpper() : key.ToLower(); |
|||
} |
|||
|
|||
PreviewTextBox.Text += key; |
|||
} |
|||
} |
|||
|
|||
private void Backspace_Click(object sender, RoutedEventArgs e) |
|||
{ |
|||
if (!string.IsNullOrEmpty(PreviewTextBox.Text)) |
|||
{ |
|||
PreviewTextBox.Text = PreviewTextBox.Text.Substring(0, PreviewTextBox.Text.Length - 1); |
|||
} |
|||
} |
|||
|
|||
private void Enter_Click(object sender, RoutedEventArgs e) |
|||
{ |
|||
KeyValue = PreviewTextBox.Text; |
|||
this.Close(); |
|||
// PreviewTextBox.Text += Environment.NewLine;
|
|||
} |
|||
|
|||
private void Hide_Click(object sender, RoutedEventArgs e) |
|||
{ |
|||
this.Close(); |
|||
} |
|||
|
|||
} |
|||
} |
Loading…
Reference in new issue