雖說輸入法不是什么新事物,各種語言版本都有,不過在c#不常見;這就會給人一種誤會:c#不能做!其實c#能不能做呢,答案是肯定的——三種方式都行:imm、tsf以及外掛式。imm這種就是調windows的一些底層api,不過在新版本的windows中基本上已經不能用了,屬于一種過時的操作方式。tsf是微軟推薦的一種新方式,不過相對c#資料太少;線上主要的一些都是針對c++的版本資料,當然可以作為借鑒來實現c#版的。我這里主要介紹一種外掛式的(天啦擼,c#可以寫外掛?),對于高手來說肯定不值一提,不過也算是實現了外掛及輸入法!題外話——c#可以做外掛么?答案是可以的,c#針對windows的api編程資料還是很多的,下面就簡單的介紹一下面可能要使用到的api:
安裝了一個鉤子,截取鼠標鍵盤等信號
1
|
public static extern int setwindowshookex( int idhook, hookproc lpfn, intptr hinstance, int threadid); |
停止使用鉤子
public static extern bool unhookwindowshookex(int idhook);
通過信息鉤子繼續下一個鉤子
public static extern int callnexthookex(int idhook, int ncode, int32 wparam, intptr lparam);
線程鉤子需要用到
static extern int getcurrentthreadid();
使用windows api函數代替獲取當前實例的函數,防止鉤子失效
public static extern intptr getmodulehandle(string name);
轉換指定的虛擬鍵碼和鍵盤狀態的相應字符或字符
1
2
3
4
5
|
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基本上就可能實現鼠標鍵盤的監控或者鎖定等;那么首先要安裝鉤子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// 安裝鍵盤鉤子 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.安裝完后就要對獲取到鉤子進行處理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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
2
3
4
5
6
7
8
9
10
11
|
public void stop() { bool retkeyboard = true ; if (hkeyboardhook != 0) { retkeyboard = unhookwindowshookex(hkeyboardhook); hkeyboardhook = 0; } if (!(retkeyboard)) throw new exception( "卸載鉤子失敗!" ); } |
4.注冊事件
1
2
3
4
5
6
|
private void wordboard_load( object sender, eventargs e) { program.keybordhook.keyupevent += keybordhook_keyupevent; program.keybordhook.onspaced += keybordhook_onspaced; program.keybordhook.onbacked += keybordhook_onbacked; } |
5.根據輸入內容顯示并進行轉換
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
private void showcharatar() { this .listview1.begininvoke( new action(() => { label1.text = keys; try { this .listview1.items.clear(); var arr = cachehelper. get (keys); if (arr != null ) for ( int i = 0; i < (arr.length > 10 ? 9 : arr.length); i++) { this .listview1.items.add((i + 1) + "、" + arr[i]); } } catch { label1.text = keys = "" ; } })); } |
6.顯示輸入
1
2
3
4
5
|
private void keybordhook_keyupevent( object sender, keyeventargs e) { keys += e.keycode.tostring().tolower(); this .showcharatar(); } |
7.空格上屏
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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.將數據發送到激活的輸入框中
1
2
3
4
5
6
7
8
9
|
public void send( string msg) { if (! string .isnullorempty(msg)) { stop(); sendkeys.send( "{right}" + msg); start(); } } |
9.back鍵回退
1
2
3
4
5
6
7
8
|
private void keybordhook_onbacked() { if (! string .isnullorempty(keys)) { keys = keys.substring(0, keys.length - 1); } this .showcharatar(); } |
當然這里還可以使其他鍵來完善更多的功能,例如拼音的分頁處理等
至于什么五筆、拼音就要使用詞庫來解決了;其中五筆比較簡單,拼音就非常復雜了,各種分詞、聯想等...這里以五筆為主,拼音為單拼來實現基本的輸入功能;所以不需要什么高深算法,簡單使用memorycache就輕松高效搞定(有興趣的可以來https://github.com/yswenli/wenli.iem 上完善)
10.鍵詞轉換
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/***************************************************************************************************** * 本代碼版權歸@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 + "\\win32\\world.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 + "\\win32\\pinyin.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); } } } |
到此一個基本型的c#版外掛輸入法就成功完成了,源碼地址:https://github.com/yswenli/wenli.iem
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/yswenli/p/6528447.html