C#自定义键盘
一、创建自定义键盘DLL文件
1、新建一个类库
2、在类库项目中添加用户控件类
3、在用户控件上添加键盘需要的控件
4、添加如下代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace UserControlLibrary
{public partial class Keyboard : UserControl{public Keyboard(){InitializeComponent();}//字符键点击事件public event Action<object, KeyboardEventArgs> CharKeyClickEvent;//功能键点击事件public event Action<object, KeyboardEventArgs> FunKeyClickEvent;/// <summary>/// 点击按钮事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btn_Key_Click(object sender, EventArgs e){Button btnKey = sender as Button;if(btnKey.Text.Length==1){CharKeyClickEvent?.Invoke(btnKey, new KeyboardEventArgs(){KeyChar = btnKey.Text});}else{FunKeyClickEvent?.Invoke(btnKey, new KeyboardEventArgs(){KeyChar=btnKey.Text});}}/// <summary>/// 大写切换/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnKeyUpper_Click(object sender, EventArgs e){Button upperButton = sender as Button;if (upperButton.Text == "Upper"){foreach (var item in this.panelKeyboard.Controls){if (item.GetType().Name != "Button")continue;Button button = item as Button;if (button.Text.Length != 1)continue;char keyChar = button.Text.ToCharArray()[0];if (keyChar >= 'A' && keyChar <= 'Z'){button.Text = button.Text.ToLower();}}upperButton.Text = "Lower";}else{foreach (var item in this.panelKeyboard.Controls){if (item.GetType().Name != "Button")continue;Button button = item as Button;if (button.Text.Length != 1)continue;char keyChar = button.Text.ToCharArray()[0];if (keyChar >= 'a' && keyChar <= 'z'){button.Text = button.Text.ToUpper();}}upperButton.Text = "Upper";}}}/// <summary>/// 参数类/// </summary>public class KeyboardEventArgs : EventArgs{public string KeyChar { get; set; }}
}
5、点击生成解决方案,得到如下的DLL文件
二、测试键盘
1、新建一个项目,在工具箱上右击点击选择项
2、点击浏览
3、找到第一步中生成的DLL文件
4、控件被添加到工具箱
5、将Keyboard控件拖到项目的Form窗体内,并添加两个TextBox控件,这里只针对TextBox控件进行输入,其他控件可以后续扩展
6、添加如下代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UserControlLibrary;namespace TestControls
{public partial class Form1 : Form{public Form1(){InitializeComponent();selectedTextBox = this.textBox;selectedTextBox.Focus();}TextBox selectedTextBox;//被选中的TextBox控件int TextBoxCursorPos=0;//TextBox控件的光标位置/// <summary>/// 点击字符键事件/// </summary>/// <param name="arg1"></param>/// <param name="arg2"></param>private void keyboard1_KeyClickEvent(object arg1, KeyboardEventArgs arg2){if(selectedTextBox==null)return;//非选择性输入if(selectedTextBox.SelectionLength==0){selectedTextBox.Text = selectedTextBox?.Text.Insert(TextBoxCursorPos, arg2.KeyChar);TextBoxCursorPos++;}else//选择性输入{int selectLength = selectedTextBox.SelectionLength;int selectStart = selectedTextBox.SelectionStart;selectedTextBox.Text = selectedTextBox.Text.Remove(selectStart, selectLength);selectedTextBox.Text = selectedTextBox.Text.Insert(selectStart, arg2.KeyChar);TextBoxCursorPos = selectStart+1;}selectedTextBox.Select(TextBoxCursorPos, 0);selectedTextBox.Focus();}/// <summary>/// 点击功能键事件/// </summary>/// <param name="arg1"></param>/// <param name="arg2"></param>private void keyboard1_FunKeyClickEvent(object arg1, KeyboardEventArgs arg2){if(selectedTextBox==null)return;switch(arg2.KeyChar){case "Delete":DeleteFunc();break;case "Clear":ClearFunc();break;default:break;}}/// <summary>/// 删除功能/// </summary>private void DeleteFunc(){//如果文本框内容为空,则锁定光标后返回if (selectedTextBox.Text.Length == 0){selectedTextBox.Select(TextBoxCursorPos, 0);selectedTextBox.Focus();return;} //非选择删除就删除光标前一位if(selectedTextBox.SelectionLength==0){int selectStart = selectedTextBox.SelectionStart;if (selectStart <= 0){selectedTextBox.Select(TextBoxCursorPos, 0);selectedTextBox.Focus();return;}selectedTextBox.Text = selectedTextBox.Text.Remove(selectStart-1, 1);TextBoxCursorPos = selectStart-1;}else//选择删除就删除选择的字符串{int selectLength = selectedTextBox.SelectionLength;int selectStart = selectedTextBox.SelectionStart;selectedTextBox.Text = selectedTextBox.Text.Remove(selectStart, selectLength);TextBoxCursorPos = selectStart;}selectedTextBox.Select(TextBoxCursorPos, 0);selectedTextBox.Focus();}/// <summary>/// 清空功能/// </summary>private void ClearFunc(){selectedTextBox.Clear();TextBoxCursorPos = selectedTextBox.SelectionStart;}/// <summary>/// 获取光标位置/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void textBox_MouseDown(object sender, MouseEventArgs e){selectedTextBox = sender as TextBox;TextBoxCursorPos = selectedTextBox.SelectionStart;}}
}
7、Keyboard有自定义的两个委托(事件):CharKeyClickEvent(字符)和FunKeyClickEvent(功能),将两个对应的事件分别添加到对应的委托。
三、测试效果
还算勉强实用的小工具键盘,工控上位机开发的时候用的!