Visual studio之C#实现数字输入模拟键盘

背景

当前做的一个上位机需运行在工控机上,众所周知,工控机不可能配备键盘,只能是触屏,而我当前的上位机需要输入参数,于是之前的解决办法既是调用Windows自带的OSK.exe虚拟键盘,方法在我的另一外一篇博客Visual studio之C# 调用系统软键盘(外部"osk.exe")中已详述,但这种做法有两个致命缺陷,一是由于调用了外部.exe程序,国产杀毒软件会将我的上位机判定为疑似病毒文件(手动微笑);二是,若是该虚拟键盘被提前打开,我就没办法继续打开该软键盘使其在我的上位机界面之上,而该软键盘不是由我打开,我就获取不到它的Process,也就无法将其关闭重新打开,工控机的界面通常不会出现任务栏,因此虚拟键盘要是没有实体键盘就永远无法打开,头疼至极。于是就想到自己实现软键盘。便有此文,做个记录。
首先强调一下,本软键盘非真正意义的软键盘,只是用了很笨的方法在同一个界面上提供了一个数字输入的解决方案,实际效果如下:

正文

  • 为了不让按键在点击的时候获取焦点,所以我采用了label控件,一共12个控件,分别是0,1...9,退格,清除,对应的name分别为,keyb0,keyb1,```keb9,keybback,keybclear,对应的Text就是相应的0,1,2...9,退格,清除,为了调整所有按键大小,属性Autosize应该设置为false

  • 在窗体Load事件中,添加所有label控件的单击事件mybutton_clicked

        private void lazerctrl_Load(object sender, EventArgs e)
        {
            // 注册键盘按键单击事件
            keyb1.Click += new EventHandler(mybutton_clicked);
            keyb2.Click += new EventHandler(mybutton_clicked);
            keyb3.Click += new EventHandler(mybutton_clicked);
            keyb4.Click += new EventHandler(mybutton_clicked);
            keyb5.Click += new EventHandler(mybutton_clicked);
            keyb6.Click += new EventHandler(mybutton_clicked);
            keyb7.Click += new EventHandler(mybutton_clicked);
            keyb8.Click += new EventHandler(mybutton_clicked);
            keyb9.Click += new EventHandler(mybutton_clicked);
            keyb0.Click += new EventHandler(mybutton_clicked);
            keybback.Click += new EventHandler(mybutton_clicked);
            keybclear.Click += new EventHandler(mybutton_clicked);
        }

在鼠标按键事件中,将控件代表的数字值,填入到页面上最后一个获取到焦点的TextBox,内。这个lastfocustextbox 后面说明,

        // 执行按键操作
        void mybutton_clicked(object sender, EventArgs e)
        {
            Label clickbtn = (Label)sender;
            string strkey = clickbtn.Text;

            if ((lastfocustextbox == textBoxmhpU) || (lastfocustextbox == textBoxmhpD)
                || (lastfocustextbox == textBoxCWhpU) || (lastfocustextbox == textBoxCWhpD)
                || (lastfocustextbox == textBoxmptU) || (lastfocustextbox == textBoxmptD)
                || (lastfocustextbox == textBoxCWptU) || (lastfocustextbox == textBoxCWptD))
            {//不可编辑的textbox就跳过
            }
            else
            {

                if (strkey == "退格")
                {
                    string strNew = this.lastfocustextbox.Text;
                    if (strNew.Length >= 1)
                    {
                        strNew = strNew.Substring(0, strNew.Length - 1);
                        this.lastfocustextbox.Text = strNew;
                        this.lastfocustextbox.SelectionStart = this.lastfocustextbox.Text.Length;
                    }
                    this.lastfocustextbox.Focus();
                }
                else if (strkey == "清除")
                {
                    lastfocustextbox.Clear();
                }
                else
                {
                    lastfocustextbox.AppendText(strkey);
                }
            }
        }
  • 为了获知当前聚焦在哪个TextBox,就需要在窗体Load事件注册所有TextBoxEnter事件,并在事件中将最后一次获得聚焦的TextBox赋值给前文提及的lastfocustextbox 中,所以我说这是笨方法。: )
        private void lazerctrl_Load(object sender, EventArgs e)
        {
            // 注册textbox获取焦点事件
            textBoxmp.Enter += new EventHandler(text_getfocused);
            textBoxhz.Enter += new EventHandler(text_getfocused);
            textBoxduty.Enter += new EventHandler(text_getfocused);
            textBoxmlpU.Enter += new EventHandler(text_getfocused);
            textBoxmtU.Enter += new EventHandler(text_getfocused);
            textBoxmlpD.Enter += new EventHandler(text_getfocused);
            textBoxmtD.Enter += new EventHandler(text_getfocused);
            textBoxCWp.Enter += new EventHandler(text_getfocused);
            textBoxCWlpU.Enter += new EventHandler(text_getfocused);
            textBoxCWtU.Enter += new EventHandler(text_getfocused);
            textBoxCWlpD.Enter += new EventHandler(text_getfocused);
            textBoxCWtD.Enter += new EventHandler(text_getfocused);
            textBoxalertt.Enter += new EventHandler(text_getfocused);
            textBoxintervalt.Enter += new EventHandler(text_getfocused);

            textBoxmhpU.Enter += new EventHandler(text_getfocused);
            textBoxmhpD.Enter += new EventHandler(text_getfocused);
            textBoxCWhpU.Enter += new EventHandler(text_getfocused);
            textBoxCWhpD.Enter += new EventHandler(text_getfocused);
            textBoxmptU.Enter += new EventHandler(text_getfocused);
            textBoxmptD.Enter += new EventHandler(text_getfocused);
            textBoxCWptU.Enter += new EventHandler(text_getfocused);
            textBoxCWptD.Enter += new EventHandler(text_getfocused);
        }

此处注意lastfocustextbox 在窗体导入时,一定要赋值一个默认的TextBox,否则在未有一个TextBox获取焦点的情况下,用户点击按键,导致操作null,抛出异常,甚至直接使UI异常关闭。

        // 声明
        private System.Windows.Forms.TextBox lastfocustextbox;
        // 赋初始值
        lastfocustextbox = textBoxintervalt;
        // 获取最后一次获取焦点的Textbox
        void text_getfocused(object sender, EventArgs e)
        {
            TextBox focuedbox = (TextBox)sender;
            lastfocustextbox = focuedbox;
        }
  • 至此,按键的功能就可以实现了,但Lable毕竟不是按键,没有Button的效果,所以我们可以继续的为lable注册鼠标按下和弹起事件,在该事件内改变lable的边框,颜色,甚至是图片,使其有按键按下和弹起的效果,特别可以选一些按键按下和弹起的图片,那样效果会相当不错。同样,在窗体Load事件注册事件
        private void lazerctrl_Load(object sender, EventArgs e)
        {
            // 注册键盘按键鼠标按下事件
            keyb1.MouseDown += new MouseEventHandler(mybutton_down);
            keyb2.MouseDown += new MouseEventHandler(mybutton_down);
            keyb3.MouseDown += new MouseEventHandler(mybutton_down);
            keyb4.MouseDown += new MouseEventHandler(mybutton_down);
            keyb5.MouseDown += new MouseEventHandler(mybutton_down);
            keyb6.MouseDown += new MouseEventHandler(mybutton_down);
            keyb7.MouseDown += new MouseEventHandler(mybutton_down);
            keyb8.MouseDown += new MouseEventHandler(mybutton_down);
            keyb9.MouseDown += new MouseEventHandler(mybutton_down);
            keyb0.MouseDown += new MouseEventHandler(mybutton_down);
            keybback.MouseDown += new MouseEventHandler(mybutton_down);
            keybclear.MouseDown += new MouseEventHandler(mybutton_down);

            // 注册键盘按键鼠标弹起事件
            keyb1.MouseUp += new MouseEventHandler(mybutton_Up);
            keyb2.MouseUp += new MouseEventHandler(mybutton_Up);
            keyb3.MouseUp += new MouseEventHandler(mybutton_Up);
            keyb4.MouseUp += new MouseEventHandler(mybutton_Up);
            keyb5.MouseUp += new MouseEventHandler(mybutton_Up);
            keyb6.MouseUp += new MouseEventHandler(mybutton_Up);
            keyb7.MouseUp += new MouseEventHandler(mybutton_Up);
            keyb8.MouseUp += new MouseEventHandler(mybutton_Up);
            keyb9.MouseUp += new MouseEventHandler(mybutton_Up);
            keyb0.MouseUp += new MouseEventHandler(mybutton_Up);
            keybback.MouseUp += new MouseEventHandler(mybutton_Up);
            keybclear.MouseUp += new MouseEventHandler(mybutton_Up);
        }
        // 执行按键鼠标按下操作
        void mybutton_down(object sender, EventArgs e)
        {
            Label clickbtn = (Label)sender;
            clickbtn.BackColor = System.Drawing.SystemColors.ActiveCaption;
            clickbtn.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        }

        // 执行按键鼠标弹起操作
        void mybutton_Up(object sender, EventArgs e)
        {
            Label clickbtn = (Label)sender;
            clickbtn.BackColor = System.Drawing.Color.Aqua;
            clickbtn.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        }

至此,记录完毕。

参考链接

  • 参考了很多,记不起来了,这么实现的灵感就来自于各种搜查的各类信息

记录时间:2017-6-11
记录地点:江苏淮安

posted @ 2017-06-11 01:40  Ch_Y_Q  阅读(4653)  评论(0编辑  收藏  举报