コード例 #1
0
        /// <summary>
        /// 输入接受字符
        /// </summary>
        /// <param name="keyCode"></param>
        private void SetInputKey(int keyCode)
        {
            string inputStr       = Convert.ToChar(keyCode).ToString().ToLower();//输入字母
            string inputStrNumber = string.Empty;

            //输入法输入切换
            if (keyCode == (int)Keys.F1 || keyCode == (int)Keys.F2 || keyCode == (int)Keys.F3 || keyCode == (int)Keys.F4 || keyCode == (int)Keys.F5)
            {
                keyStack.Clear();//清空原先输入内容
                keyNumberList.Clear();
                InitVariable();
            }
            else if (keyCode == (int)Keys.F6)//小数点
            {
                if (inputType == InputMethodType.InputType.CN9 || inputType == InputMethodType.InputType.CN)
                {
                    SendResult("。");
                }
                else
                {
                    SendResult(".");
                }
            }
            else if (keyCode == (int)Keys.Enter)
            {
                if (ListResult.Count == 0)//发送回车键
                {
                    KeybdEvent(Keys.Enter);
                }
            }
            else if (keyCode == (int)Keys.Space)
            {
                if (ListResult.Count == 0)//发送空格
                {
                    KeybdEvent(Keys.Space);
                }
            }
            else if (keyCode > 97 && keyCode <= 105 && inputType == InputMethodType.InputType.CN9) //CN9输入方式
            {
                inputStrNumber = Convert.ToChar(keyCode - 48).ToString().ToLower();                //获得键盘NO
                SetGroupPinyin(Convert.ToInt32(inputStrNumber));
                textBoxInput.Text = GetGroupPinyin();
                keyNumberList.Add(Convert.ToInt32(inputStrNumber));
            }
            else if (keyCode > 97 && keyCode <= 105 && (inputType == InputMethodType.InputType.EN || inputType == InputMethodType.InputType.ENUpper) &&
                     keyNumberList.Count == 0)                                      //英文,每次只允许录入一个
            {
                inputStrNumber = Convert.ToChar(keyCode - 48).ToString().ToLower(); //获得键盘NO
                StringBuilder sb    = new StringBuilder();
                int           count = 1;
                foreach (string item in KeyDic[Convert.ToInt32(inputStrNumber)])
                {
                    Word w = new Word();
                    if (this.inputType == InputMethodType.InputType.ENUpper)
                    {
                        w.Words = item.ToUpper();
                    }
                    else
                    {
                        w.Words = item.ToLower();
                    }
                    w.Counts = 0;
                    ListResult.Add(w);
                    sb.Append(count + "." + w.Words + " ");
                    count++;
                }
                listBoxResult.Items.Add(sb.ToString());
                endIndex = count - 1;
                keyNumberList.Add(Convert.ToInt32(inputStrNumber));
            }
            else if (keyCode >= 96 && keyCode <= 105 && inputType == InputMethodType.InputType.NO) //数字
            {
                inputStrNumber = Convert.ToChar(keyCode - 48).ToString().ToLower();                //获得键盘NO
                _resultStr     = inputStrNumber;
                KeybdEvent((Keys)keyCode);
            }
            else if (keyCode == 96 && inputType != InputMethodType.InputType.NO)//空格按键
            {
                KeybdEvent(Keys.Space);
            }
            else if (inputType == InputMethodType.InputType.CN)//全键盘
            {
                if (keyCode != (int)Keys.Back && keyCode != (int)Keys.Space && keyCode != (int)Keys.Enter)
                {
                    textBoxInput.Text = CommonFun.GetEnterPinyin(inputStr, textBoxInput.Text);
                }
                FindString(new string[] { textBoxInput.Text });
            }
            else
            {
                // KeybdEvent((Keys)keyCode);
                SendResult(inputStr);
            }
        }
コード例 #2
0
        /// <summary>
        /// 获取CN9
        /// </summary>
        /// <param name="keyIndex"></param>
        private void SetGroupPinyin(int keyIndex)
        {
            int    tempCount = 0;
            string result    = string.Empty;

            listBoxGroup.Items.Clear();
            List <string> tempList = new List <string>();

            if (keyStack.Count <= 0)//正常入栈
            {
                for (int i = KeyDic[keyIndex].Length - 1; i >= 0; i--)
                {
                    if (KeyDic[keyIndex][i] != "u" && KeyDic[keyIndex][i] != "i" && KeyDic[keyIndex][i] != "v")
                    {
                        keyStack.Push(KeyDic[keyIndex][i]);
                        if (IsDebug)
                        {
                            listBoxGroup.Items.Add(KeyDic[keyIndex][i]);
                        }
                    }
                }
            }
            else
            {
                string currentStr = string.Empty;
                do
                {
                    string strall = keyStack.Pop();
                    foreach (string inputStr in KeyDic[keyIndex])
                    {
                        int index = 0;
                        currentStr = CommonFun.GetEnterPinyin(inputStr, strall);
                        tempCount  = CommonFun.GetCount(currentStr);
                        if (!tempList.Contains(currentStr))
                        {
                            //按照拼音个数降序
                            #region 排序算法,比较慢
                            //for (int i = 0; i < tempList.Count; i++)
                            //{
                            //    if (CommonFun.GetCount(tempList[i]) >= tempCount)
                            //        index = i;
                            //}
                            //if (index > 0 && index == tempList.Count - 1)
                            //    tempList.Add(currentStr);
                            //else
                            //    tempList.Insert(index, currentStr);
                            #endregion
                            //修改为二分法
                            index = CommonFun.FindIndex(tempList, tempCount);
                            tempList.Insert(index, currentStr);
                        }
                    }
                } while (keyStack.Count > 0);

                for (int i = tempList.Count - 1; i >= 0; i--)
                {
                    string[] tempArr = CommonFun.StringSplitToArr(tempList[i]);
                    if (tempArr.Length > 2 && (tempArr[tempArr.Length - 1].Length <= 1 && tempArr[tempArr.Length - 2].Length <= 1))
                    {
                        continue;
                    }
                    keyStack.Push(tempList[i]);
                    if (IsDebug)
                    {
                        listBoxGroup.Items.Add(tempList[i]);
                    }
                }
                #region 过滤不必要的组合 之前逻辑干掉
                //  List<string> tempList = new List<string>();
                //foreach (string sResult in tempKeyStack)
                //{
                //    tempCount = CommonFun.GetCount(sResult);
                //    string[] tempArr = CommonFun.StringSplitToArr(sResult);

                //    //单个词并且,不能多音节组成词语或者最后2位都是单个字
                //    if (tempCount > 0 && tempArr.Length > 1 && (tempArr[0].Length <= 1
                //      || (tempArr[tempArr.Length - 1].Length <= 1 && tempArr[tempArr.Length - 2].Length <= 1)
                //    ))
                //    {
                //        continue;
                //    }
                //    //2个以上切分,并且最后一位单独--去除
                //    if (tempArr.Length > 2 && tempArr[tempArr.Length - 1].Length <= 1)
                //    {
                //        continue;
                //    }
                //    //单个完整字往前排
                //    if (!tempList.Contains(sResult))
                //    {
                //        int index = CommonFun.GetListIndex(tempList, sResult);
                //        if (index == tempList.Count - 1)
                //        {
                //            tempList.Add(sResult);
                //        }
                //        else
                //        {
                //            tempList.Insert(index, sResult);
                //        }
                //    }
                //}

                //foreach (string item in tempList)
                //{
                //    keyStack.Push(item);
                //    listBoxGroup.Items.Add(item);
                //}
                #endregion
            }
        }