您好,欢迎访问一九零五行业门户网

C# 关于输入法的介绍

c# 输入法
    虽说输入法不是什么新事物,各种语言版本都有,不过在c#不常见;这就会给人一种误会:c#不能做!其实c#能不能做呢,答案是肯定的——三种方式都行:imm、tsf以及外挂式。imm这种就是调windows的一些底层api,不过在新版本的windows中基本上已经不能用了,属于一种过时的操作方式。tsf是微软推荐的一种新方式,不过相对c#资料太少;线上主要的一些都是针对c++的版本资料,当然可以作为借鉴来实现c#版的。我这里主要介绍一种外挂式的(天啦撸,c#可以写外挂?),对于高手来说肯定不值一提,不过也算是实现了外挂及输入法!题外话——c#可以做外挂么?答案是可以的,c#针对windows的api编程资料还是很多的,下面就简单的介绍一下面可能要使用到的api:
安装了一个钩子,截取鼠标键盘等信号
public static extern int setwindowshookex( int idhook, hookproc lpfn, intptr hinstance, int threadid);
停止使用钩子
publicstatic extern bool unhookwindowshookex(intidhook);
通过信息钩子继续下一个钩子
publicstatic extern int callnexthookex(intidhook, int ncode, int32 wparam, intptr lparam);
线程钩子需要用到
static extern int getcurrentthreadid();
使用windows api函数代替获取当前实例的函数,防止钩子失效
publicstatic extern intptr getmodulehandle(stringname);
转换指定的虚拟键码和键盘状态的相应字符或字符
public static extern int toascii(int uvirtkey, //[in] 指定虚拟关键代码进行翻译。
int uscancode, // [in] 指定的硬件扫描码的关键须翻译成英文。高阶位的这个值设定的关键,如果是(不压)
byte[] lpbkeystate, // [in] 指针,以256字节数组,包含当前键盘的状态。每个元素(字节)的数组包含状态的一个关键。如果高阶位的字节是一套,关键是下跌(按下)。在低比特,如果设置表明,关键是对切换。在此功能,只有肘位的caps lock键是相关的。在切换状态的num个锁和滚动锁定键被忽略。
byte[] lpwtranskey, // [out] 指针的缓冲区收到翻译字符或字符。
int fustate);
1.有了以上的这些api基本上就可能实现鼠标键盘的监控或者锁定等;那么首先要安装钩子:
// 安装键盘钩子 public void start() { if (hkeyboardhook == 0) { keyboardhookprocedure = new hookproc(keyboardhookproc); hkeyboardhook = setwindowshookex(wh_keyboard_ll, keyboardhookprocedure, getmodulehandle(process.getcurrentprocess().mainmodule.modulename), 0); //如果setwindowshookex失败 if (hkeyboardhook == 0) { stop(); throw new exception("安装键盘钩子失败"); } } }
2.安装完后就要对获取到钩子进行处理:
private int keyboardhookproc(int ncode, int32 wparam, intptr lparam) { // 侦听键盘事件 if (ncode >= 0 && wparam == 0x0100) { keyboardhookstruct mykeyboardhookstruct = (keyboardhookstruct)marshal.ptrtostructure(lparam, typeof(keyboardhookstruct)); #region 开关 if (mykeyboardhookstruct.vkcode == 20 || mykeyboardhookstruct.vkcode == 160 || mykeyboardhookstruct.vkcode == 161) { islocked = islocked ? false : true; } #endregion #region if (islocked) { if (isstarted && mykeyboardhookstruct.vkcode >= 48 && mykeyboardhookstruct.vkcode <= 57) { var c = int.parse(((char)mykeyboardhookstruct.vkcode).tostring()); onspaced(c); isstarted = false; return 1; } if (isstarted && mykeyboardhookstruct.vkcode == 8) { onbacked(); return 1; } if ((mykeyboardhookstruct.vkcode >= 65 && mykeyboardhookstruct.vkcode <= 90) || mykeyboardhookstruct.vkcode == 32) { if (mykeyboardhookstruct.vkcode >= 65 && mykeyboardhookstruct.vkcode <= 90) { keys keydata = (keys)mykeyboardhookstruct.vkcode; keyeventargs e = new keyeventargs(keydata); keyupevent(this, e); isstarted = true; } if (mykeyboardhookstruct.vkcode == 32) { onspaced(0); isstarted = false; } return 1; } else return 0; } #endregion } return callnexthookex(hkeyboardhook, ncode, wparam, lparam); }
上面一些数字,对于刚入门的同学来说也不是什么问题,一看就明白是对哪些键做的操作。
3.停止钩子
1 public void stop() 2 { 3 bool retkeyboard = true; 4 5 6 if (hkeyboardhook != 0) 7 { 8 retkeyboard = unhookwindowshookex(hkeyboardhook); 9 hkeyboardhook = 0;10 }11 12 if (!(retkeyboard))13 throw new exception("卸载钩子失败!");14 }
4.注册事件
1 private void wordboard_load(object sender, eventargs e)2 {3 program.keybordhook.keyupevent += keybordhook_keyupevent;4 program.keybordhook.onspaced += keybordhook_onspaced;5 program.keybordhook.onbacked += keybordhook_onbacked;6 }
5.根据输入内容显示并进行转换
1 private void showcharatar() 2 { 3 this.listview1.begininvoke(new action(() => 4 { 5 label1.text = keys; 6 7 try 8 { 9 this.listview1.items.clear();10 var arr = cachehelper.get(keys);11 if (arr != null)12 for (int i = 0; i < (arr.length > 10 ? 9 : arr.length); i++)13 {14 this.listview1.items.add((i + 1) + "、" + arr[i]);15 }16 }17 catch18 {19 label1.text = keys = "";20 }21 }));22 }
6.显示输入
1 private void keybordhook_keyupevent(object sender, keyeventargs e)2 {3 keys += e.keycode.tostring().tolower();4 this.showcharatar();5 }
7.空格上屏
1 private void keybordhook_onspaced(int choose) 2 { 3 try 4 { 5 if (cachehelper.containskey(keys)) 6 { 7 if (choose > 0) 8 { 9 choose = choose - 1;10 }11 12 program.keybordhook.send(cachehelper.get(keys)[choose]);13 label1.text = "";14 this.listview1.clear();15 }16 }17 catch18 {19 20 }21 keys = "";22 }
8.将数据发送到激活的输入框中
1 public void send(string msg)2 {3 if (!string.isnullorempty(msg))4 {5 stop();6 sendkeys.send("{right}" + msg);7 start();8 }9 }
9.back键回退
1 private void keybordhook_onbacked()2 {3 if (!string.isnullorempty(keys))4 {5 keys = keys.substring(0, keys.length - 1);6 }7 this.showcharatar();8 }
当然这里还可以使其他键来完善更多的功能,例如拼音的分页处理等
至于什么五笔、拼音就要使用词库来解决了;其中五笔比较简单,拼音就非常复杂了,各种分词、联想等...这里以五笔为主,拼音为单拼来实现基本的输入功能;所以不需要什么高深算法,简单使用memorycache就轻松高效搞定
10.键词转换
1 /***************************************************************************************************** 2 * 本代码版权归@wenli所有,all rights reserved (c) 2015-2017 3 ***************************************************************************************************** 4 * clr版本:4.0.30319.42000 5 * 唯一标识:8ebc884b-ee5f-45de-8638-c054b832e0ce 6 * 机器名称:wenli-pc 7 * 联系人邮箱:wenguoli_520@qq.com 8 ***************************************************************************************************** 9 * 项目名称:$projectname$ 10 * 命名空间:wenli.iem 11 * 类名称:cachehelper 12 * 创建时间:2017/3/3 16:18:14 13 * 创建人:wenli 14 * 创建说明: 15 *****************************************************************************************************/ 16 using system; 17 using system.collections.generic; 18 using system.io; 19 using system.linq; 20 using system.runtime.caching; 21 using system.text; 22 using system.windows.forms; 23 24 namespace wenli.iem.helper 25 { 26 public static class cachehelper 27 { 28 static memorycache _wubicache = new memorycache("wubi"); 29 30 static memorycache _pinyincache = new memorycache("pinyin"); 31 32 static cachehelper() 33 { 34 var path = application.startuppath + "\\win32\\world.dll"; 35 var arr = file.readalllines(path); 36 foreach (string item in arr) 37 { 38 var key = item.substring(0, item.indexof(" ")); 39 var value = item.substring(item.indexof(" ") + 1); 40 _wubicache.add(key, (object)value, datetimeoffset.maxvalue); 41 } 42 43 // 44 45 path = application.startuppath + "\\win32\\pinyin.dll"; 46 arr = file.readalllines(path); 47 foreach (string item in arr) 48 { 49 var key = item.substring(0, item.indexof(" ")); 50 var value = item.substring(item.indexof(" ") + 1); 51 _pinyincache.add(key, (object)value, datetimeoffset.maxvalue); 52 } 53 } 54 55 public static string[] get(string key) 56 { 57 if (!string.isnullorempty(key)) 58 { 59 var str = string.empty; 60 61 try 62 { 63 if (_wubicache.contains(key)) 64 str = _wubicache[key].tostring(); 65 } 66 catch { } 67 try 68 { 69 if (_pinyincache.contains(key)) 70 str += " " + _pinyincache[key].tostring(); 71 } 72 catch { } 73 74 if (!string.isnullorempty(str)) 75 { 76 var arr = str.split(new string[] { " " }, stringsplitoptions.removeemptyentries); 77 for (int i = 0; i < arr.length; i++) 78 { 79 if (arr[i].indexof("*") > -1) 80 { 81 arr[i] = arr[i].substring(0, arr[i].indexof("*")); 82 } 83 } 84 return arr; 85 } 86 } 87 88 return null; 89 } 90 91 92 public static bool containskey(string key) 93 { 94 if (_wubicache.contains(key)) 95 return true; 96 if (_pinyincache.contains(key)) 97 return true; 98 return false; 99 }100 101 public static void clear()102 {103 _wubicache.dispose();104 gc.collect(-1);105 }106 }107 }
到此一个基本型的c#版外挂输入法就成功完成了
以上就是c# 关于输入法的介绍的详细内容。
其它类似信息

推荐信息