C#自定义键盘

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(功能),将两个对应的事件分别添加到对应的委托。
在这里插入图片描述

三、测试效果

在这里插入图片描述

在这里插入图片描述
还算勉强实用的小工具键盘,工控上位机开发的时候用的!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/1383036.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

IOS控件系列--优雅的表情键盘设计(扩展性好)

本控件效果如下图所示&#xff1a; 本控件设计的几个原则 &#xff1a; 1.扩展性好&#xff0c;代码不冗余。 2.逻辑清晰。 3.回调接口清晰。 4.移植性好。 首先来讨论下本控件的扩展性&#xff1a; 效果图给出的是3行7列。如果这个行列因子可控&#xff0c;起码可以应付…

坤音键盘(ikun专用)

坤音键位说明 &#xff08;下图第一个字母对应的是26键的键位&#xff0c;想听哪个按哪个。后面的数字代表时长&#xff0c;单位&#xff1a;秒&#xff09; 在右下角托盘里面可以右键关闭 ps&#xff1a;执行后桌面会出现一个ikun精灵&#xff0c;只要找一个能输入内容的地方…

Android自定义软键盘的实现

先看界面布局文件 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"fill_parent"android:layout_height"fill_parent"a…

Android自定义键盘(KeyboardView)

目录 1.场景&#xff1a;2.想法&#xff1a;3.开始实现&#xff1a;《一》 在res包下创建xml目录,Keyboard标签来定义键盘布局&#xff1a;《二》创建IKeyboardView类并继承KeyboardView类,设置键盘布局&#xff08;数字和字母&#xff09;《三》 处理自定义键盘按键的点击事件…

关于技术转管理角色的认知

软件质量保障&#xff1a;所寫即所思&#xff5c;一个阿里质量人对测试的所感所悟。 程序员发展的岔路口 技术人做了几年专业工作之后&#xff0c;会来到一个重要的“分岔路口”&#xff0c;一边是专业的技术路线&#xff0c;一边是技术团队的管理路线。不少人就开始犯难&…

什么是响应式设计?列举几种实现响应式设计的方法。

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 什么是响应式设计&#xff1f;⭐ 实现响应式设计的方法⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 欢迎来到前端入门之旅&#xff01;这个专栏…

【雕爷学编程】Arduino动手做(12)---霍尔模块之单极性霍尔开关器件AH3144E与线性霍尔传感器AH49E

37款传感器与模块的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&#x…

超级简单方法解决秒杀超卖和重复购买问题

一、基本原则 1、SQL加库存数量判断&#xff1a;防止库存变成负数 2.、数据库加唯一索引&#xff1a;防止用户重复购买 二、举例说明 1、开启事务 2、商品表减库存&#xff0c;库存数量要大于0 update miaosha_goods set stock_count stock_count - 1 where goods.id …

金雅拓超级狗superdog

超级狗是SafeNet在圣天诺基础上为中国本土量身定制的新一代加密狗。 超级狗操作简单&#xff0c;非常安全&#xff0c;功能实用&#xff0c;助您轻松完成高强度加密。 PDF和flv, swf, mpg, dat, avi, wav, mp3等加密&#xff09; 4、文档加密&#xff0c;ppt,word,excel,pdf&am…

赛孚耐SafeNet开发狗超级狗开发入门

本文主要讲解如果使用C#语言来对超级狗进行二次开发&#xff0c;如果仅仅是做个激活的功能&#xff0c;可以参照另一篇博客&#xff0c;地址&#xff1a;http://www.cnblogs.com/dathlin/p/8487842.html 如果疑问&#xff0c;可以加QQ群&#xff1a;592132877 继续主题研究&am…

在Elasticsearch中回测超级趋势线(Supertrend)交叉交易策略

我们已经讨论了好几个单一指标交易策略&#xff0c;其中简单的相对强弱指数&#xff08;RSI&#xff09;交易策略取得的利润最高。 在本文中&#xff0c;我们将使用 Elasticsearch 实现超级趋势线&#xff08;Supertrend&#xff09;交叉交易策略&#xff0c;并比较其性能是否优…

cmake基础(1)——简单项目

本节将围绕着hello world展开介绍如何创建一个简单的项目。 一、最小项目 1.准备工作 首先&#xff0c;新建一个目录01用来存放当前项目&#xff0c;并在目录下创建main.cpp和CMakeLists.txt文件&#xff0c;两者文件内容如下&#xff1a; #include <iostream> using…

Go把Map转成对象

最近使用了Redis的Hash&#xff0c;把一个对象给存储到了hash里面&#xff0c;具体如下&#xff1a; 现在需要从RedisHash缓存里面把结果给取出来&#xff0c;同时赋值到一个对象上面 result, err : global.GVA_REDIS.HGetAll(context.Background(), key).Result() 问题是resul…

优秀的LCD显示效果

优秀的LCD显示效果 效果图&#xff1a; 程序和源代码链接&#xff1a;http://download.csdn.net/download/zhangxiaoyu_sy/10012770

屏幕显示技术

本文主要介绍多种屏幕显示技术&#xff0c;主要是三大类&#xff0c;LCD&#xff0c;LED&#xff0c;OLED。 一、LCD LCD&#xff08;Liquid Crystal Display&#xff09;中文是液晶显示器。 LCD 的显示技术有很多&#xff0c;常见的有TFT&#xff0c;IPS&#xff0c;SLCD等…

物联网开发平台大 PK,谁是最佳 Pick?

通过这篇文章了解现下流行的几款物联网软件平台的各种功能。 作者 | Miyuru Dayarathna 译者 | 弯月&#xff0c;责编 | 郭芮 出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09; 以下为译文&#xff1a; 本文以我们对各大物联网供应商的详细分析为基础&#xff0…

uni-app 2.2 发布,大幅度优化 H5 端性能体验 | 技术头条

作者 | uni-app团队 责编 | 伍杏玲 uni-app 自发布以来&#xff0c;已经服务了几十万开发者。但让我们意外&#xff0c;有大量开发者用uni-app只编写H5版&#xff0c;并没有多端发布。 这其实也符合uni-app的初衷&#xff0c;uni-app的定位并不是需要多端发布时才用uni-app。…

第九代小冰惊喜登场,多端融合且琴棋书画样样精通

谈及智能助手&#xff0c;相信大家都不会漏过小冰这款具有划时代意义的产品。从最初的微软小冰到现在的第九代小冰&#xff0c;AI的技术在不断的演进&#xff0c;而小冰也从最初的贴心助手变成了如今琴棋书画样样精通的人工智能前沿技术载体。 北京时间2021年9月22日&#xff…

渡鸦音箱独家测评: 代表百度AI技术尊严的DuerOS, 用户体验真的能过关吗?

撰文 | 宇多田 在今年三个多小时的世界大会主论坛上&#xff0c;百度把一半时间都留给了渡鸦音箱。 这个造型极其吸睛的四色正方体&#xff0c;暂时代替了无人车&#xff0c;成为李彦宏唯一揣到兜里带到乌镇互联网大会的「百度技术代表作」。 与其他重量级 AI 硬件厂商相比&am…

机器视觉 使用halcon的学习之路

2022.9.23 公司安排说学习机器视觉。 当天下午下载安装了halcon软件。看里面的示例程序。 除了纯英文外&#xff0c;很详细的。点中算子函数&#xff0c;按F1会有详细介绍。按F5执行。按F7单步执行。我随便看看啊&#xff0c;就一直按F5 , 看效果。看不出什么名堂。 2022.9.2…