Winform custom keyboard controls the development and use

  Recently, the students presented a project to use keyboard controls, the system comes osk.exe difficult to use, so there will be the following:

  The first is to develop a custom keyboard controls, in fact, the core we all know, is to send the appropriate use SendKeys.Send

Character, but in order to do a complete, or add some other code, the specific pattern as shown below:

 

 

 

 

 

  Source as follows:

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 xktControl
{
    public enum KeyBorderCharType
    {
        CHAR = 1,
        NUMBER = 2
    }
    public partial class xktKeyBoard : UserControl
    {
        public xktKeyBoard()
        {
            InitializeComponent();
            this.tableLayoutPanel2.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            EventHandle(this);
        }



        private void EventHandle(Control ctl)
        {
            foreach (Control item in ctl.Controls)
            {
                if (item is Label lb)
                {
                    lb.MouseDown += KeyDown_MouseDown;
                }
                else if (item.HasChildren)
                {
                    EventHandle (Item); 
                } 
            } 
        } 


        [the Browsable ( to true ), the Description ( " button click event " ), the Category ( " custom property " )]
         public  Event the EventHandler KeyClick; 

        [the Browsable ( to true ), the Description ( " Enter a click event " ), the Category ( " custom property " )]
         public  Event the EventHandler EnterClick;
         ///  <Summary> 
        ///The when Occurs [Backspace clike].
         ///  </ the Summary> 
        [the Browsable ( to true ), the Description ( " delete click event " ), the Category ( " Custom Properties " )]
         public  Event EventHandler BackspaceClick;
         ///  <the Summary> 
        / // Occurs the when [retract clike].
         ///  </ the Summary> 
        [the Browsable ( to true ), the Description ( " Close click event " ), the Category ( " custom properties " )]
         public  event EventHandler closeclick;

        private void KeyDown_MouseDown(object sender, MouseEventArgs e)
        {
            if (sender is Label lbl)
            {
                if (string.IsNullOrEmpty(lbl.Text))
                {
                    return;
                }
                if (lbl.Text == "CAP")
                {
                    ToUpperOrLower(this,true);
                    lbl.Text = "cap";
                }
                else if (lbl.Text == "cap")
                {
                    ToUpperOrLower(this,false);
                    lbl.Text = "CAP";
                }
                else if (lbl.Text == "?123" || lbl.Text == "abc.")
                {
                    ChangeShow(this);
                }
                else if (lbl.Text == "空格")
                {
                    SendKeys.Send(" ");
                }
                else if (lbl.Text.ToLower() == "shift")
                {
                    SendKeys.Send("+");
                    if (lbl.Text == "shift")
                    {
                        lbl.Text = "SHIFT";
                        lbl.Tag = "SHIFT";
                    }
                    else
                    {
                        lbl.Text = "shift";
                        lbl.Tag = "shift";
                    }
                }
                else if (lbl.Text == "删除")
                {
                    SendKeys.Send("{BACKSPACE}");
                    BackspaceClick?.Invoke(sender, e);
                }
                else if (lbl.Text == "回车")
                {
                    SendKeys.Send("{ENTER}");
                    EnterClick?.Invoke(sender, e);
                }
                else if (lbl.Text == "关闭")
                {
                    CloseClick?.Invoke(this, e);
                }
                else
                {
                    string Str = "{" + lbl.Text + "}";
                    SendKeys.Send(lbl.Text);
                    KeyClick?.Invoke(sender, e);
                }
            }

        }

        private KeyBorderCharType charType = KeyBorderCharType.CHAR;

        [Browsable(true), Description("显示样式"), Category("自定义属性")]
        public KeyBorderCharType CharType
        {
            get { return charType; }
            set
            {
                charType = value;
                if (value == KeyBorderCharType.CHAR)
                {
                    if (lbl_NumChar.Text.ToLower() == "abc.")
                    {
                        ChangeShow(this);
                    }
                }
                else
                {
                    if (lbl_NumChar.Text.ToLower() == "?123")
                    {
                        ChangeShow(this);
                    }
                }
            }
        }


        private void ToUpperOrLower(Control ctl, bool bln)
        {
            foreach (Control item in ctl.Controls)
            {
                if (item is Label lbl)
                {
                    if (lbl.Text == "abc." || lbl.Text.ToLower() == "shift")
                        return;

                    lbl.Text = bln ? lbl.Text.ToUpper() : lbl.Text.ToLower();
                }
                else if (item.HasChildren)
                {
                    ToUpperOrLower(item, bln);
                }
            }
        }

        private void ChangeShow(Control ctl)
        {
            foreach (Control item in ctl.Controls)
            {
                if (item is Label lb)
                {
                    string strTag = lb.Text;
                    lb.Text = lb.Tag.ToString();
                    lb.Tag = strTag;
                }
                else if (item.HasChildren)
                {
                    ChangeShow(item);
                }
            }
        }

    }
}

Practical applications are as follows:

 

 

Guess you like

Origin www.cnblogs.com/xiketangedu/p/11589005.html