C#实现输入法功能详解

2019-12-30 16:51:20于海丽

7.空格上屏


private void KeyBordHook_OnSpaced(int choose)
  {
   try
   {
    if (CacheHelper.ContainsKey(keys))
    {
     if (choose > 0)
     {
      choose = choose - 1;
     }
     Program.keyBordHook.Send(CacheHelper.Get(keys)[choose]);
     label1.Text = "";
     this.listView1.Clear();
    }
   }
   catch
   {
   }
   keys = "";
  }

8.将数据发送到激活的输入框中


public void Send(string msg)
  {
   if (!string.IsNullOrEmpty(msg))
   {
    Stop();
    SendKeys.Send("{RIGHT}" + msg);
    Start();
   }
  }

9.back键回退


private void KeyBordHook_OnBacked()
  {
   if (!string.IsNullOrEmpty(keys))
   {
    keys = keys.Substring(0, keys.Length - 1);
   }
   this.ShowCharatar();
  }

当然这里还可以使其他键来完善更多的功能,例如拼音的分页处理等

至于什么五笔、拼音就要使用词库来解决了;其中五笔比较简单,拼音就非常复杂了,各种分词、联想等...这里以五笔为主,拼音为单拼来实现基本的输入功能;所以不需要什么高深算法,简单使用MemoryCache就轻松高效搞定(有兴趣的可以来https://www.easck.com/p>

10.键词转换


/*****************************************************************************************************
 * 本代码版权归@wenli所有,All Rights Reserved (C) 2015-2017
*****************************************************************************************************
 * CLR版本:4.0.30319.42000
 * 唯一标识:8ebc884b-ee5f-45de-8638-c054b832e0ce
 * 机器名称:WENLI-PC
 * 联系人邮箱:wenguoli_520@qq.com
*****************************************************************************************************
 * 项目名称:$projectname$
 * 命名空间:Wenli.IEM
 * 类名称:CacheHelper
 * 创建时间:2017/3/3 16:18:14
 * 创建人:wenli
 * 创建说明:
*****************************************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Windows.Forms;
namespace Wenli.IEM.Helper
{
 public static class CacheHelper
 {
  static MemoryCache _wubiCache = new MemoryCache("wubi");
  static MemoryCache _pinyinCache = new MemoryCache("pinyin");
  static CacheHelper()
  {
   var path = Application.StartupPath + "Win32world.dll";
   var arr = File.ReadAllLines(path);
   foreach (string item in arr)
   {
    var key = item.Substring(0, item.IndexOf(" "));
    var value = item.Substring(item.IndexOf(" ") + 1);
    _wubiCache.Add(key, (object)value, DateTimeOffset.MaxValue);
   }
   //
   path = Application.StartupPath + "Win32pinyin.dll";
   arr = File.ReadAllLines(path);
   foreach (string item in arr)
   {
    var key = item.Substring(0, item.IndexOf(" "));
    var value = item.Substring(item.IndexOf(" ") + 1);
    _pinyinCache.Add(key, (object)value, DateTimeOffset.MaxValue);
   }
  }
  public static string[] Get(string key)
  {
   if (!string.IsNullOrEmpty(key))
   {
    var str = string.Empty;
    try
    {
     if (_wubiCache.Contains(key))
      str = _wubiCache[key].ToString();
    }
    catch { }
    try
    {
     if (_pinyinCache.Contains(key))
      str += " " + _pinyinCache[key].ToString();
    }
    catch { }
    if (!string.IsNullOrEmpty(str))
    {
     var arr = str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
     for (int i = 0; i < arr.Length; i++)
     {
      if (arr[i].IndexOf("*") > -1)
      {
       arr[i] = arr[i].Substring(0, arr[i].IndexOf("*"));
      }
     }
     return arr;
    }
   }
   return null;
  }
  public static bool ContainsKey(string key)
  {
   if (_wubiCache.Contains(key))
    return true;
   if (_pinyinCache.Contains(key))
    return true;
   return false;
  }
  public static void Clear()
  {
   _wubiCache.Dispose();
   GC.Collect(-1);
  }
 }
}