Stop() 2 { 3 bool retKeyboard = true; (hKeyboardHook != 0) 7 { 8 retKeyboard = UnhookWindowsHookEx(hKeyboardHook); 9 hKeyboardHook = 0; 10 } (!(retKeyboard)) Exception(); 14 }
4.注册事件
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.根据输入内容显示并进行转换
ShowCharatar() 2 { 3 this.listView1.BeginInvoke(new Action(() => 4 { 5 label1.Text = keys; { 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 { + arr[i]); 15 } 16 } { 19 label1.Text = keys = ""; 20 } 21 })); 22 }
6.显示输入
KeyBordHook_KeyUpEvent(object sender, KeyEventArgs e) 2 { 3 keys += e.KeyCode.ToString().ToLower(); 4 this.ShowCharatar(); 5 }
7.空格上屏
KeyBordHook_OnSpaced(int choose) 2 { { 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 } { 19 20 } 21 keys = ""; 22 }
8.将数据发送到激活的输入框中
Send(string msg) 2 { 3 if (!string.IsNullOrEmpty(msg)) 4 { 5 Stop(); + msg); 7 Start(); 8 } 9 }
9.back键回退
KeyBordHook_OnBacked() 2 { 3 if (!string.IsNullOrEmpty(keys)) 4 { 5 keys = keys.Substring(0, keys.Length - 1); 6 } 7 this.ShowCharatar(); 8 }
当然这里还可以使其他键来完善更多的功能,例如拼音的分页处理等