c# 搜狗拼音输入法,刷输入速度和累计输入
事件起因:
搜狗拼音有幾個(gè)稱(chēng)號(hào)(光速超人:要求最快打字速度 200字/m,一代文豪:要求累計(jì)輸入字?jǐn)?shù)達(dá)200000)一直沒(méi)有那么快的速度,就想用.net來(lái)實(shí)現(xiàn)。
相關(guān)技術(shù):
1、winform基本控件使用
2、多線(xiàn)程開(kāi)發(fā)
3、C# Win32api函數(shù)調(diào)用
核心代碼
1、在窗體中放入兩個(gè)按鈕 分別名稱(chēng)為:開(kāi)始(name:btnStart) 停止(btnStop)
2、添加一個(gè)下拉框?yàn)閏bSpeend 輸入速度下拉選項(xiàng)
3、添加文本框命名為txtInWord
4、后臺(tái)需要引用命名空間
1 using System.Runtime.InteropServices;
5、導(dǎo)入鍵盤(pán)輸入方法SendInput,該方法包含了對(duì)鍵盤(pán),鼠標(biāo),硬件輸入的底層方法。定義代碼如下
1 //導(dǎo)入SendInput方法
2 [DllImport("user32.dll")]
3 public static extern UInt32 SendInput(UInt32 nInputs, ref INPUT pInputs, int cbSize);
4
5 //輸入結(jié)構(gòu)體
6 [StructLayout(LayoutKind.Explicit)]
7 public struct INPUT
8 {
9 [FieldOffset(0)]
10 public Int32 type;
11 [FieldOffset(4)]
12 public KEYBDINPUT ki;
13 [FieldOffset(4)]
14 public MOUSEINPUT mi;
15 [FieldOffset(4)]
16 public HARDWAREINPUT hi;
17 }
18
19 //鼠標(biāo)輸入結(jié)構(gòu)體
20 [StructLayout(LayoutKind.Sequential)]
21 public struct MOUSEINPUT
22 {
23 public Int32 dx;
24 public Int32 dy;
25 public Int32 mouseData;
26 public Int32 dwFlags;
27 public Int32 time;
28 public IntPtr dwExtraInfo;
29 }
30
31 //鍵盤(pán)輸入結(jié)構(gòu)體
32 [StructLayout(LayoutKind.Sequential)]
33 public struct KEYBDINPUT
34 {
35 public Int16 wVk;
36 public Int16 wScan;
37 public Int32 dwFlags;
38 public Int32 time;
39 public IntPtr dwExtraInfo;
40 }
41
42 //硬件輸入結(jié)構(gòu)體
43 [StructLayout(LayoutKind.Sequential)]
44 public struct HARDWAREINPUT
45 {
46 public Int32 uMsg;
47 public Int16 wParamL;
48 public Int16 wParamH;
49 }
50 //鍵盤(pán)輸入
51 public const int INPUT_KEYBOARD = 1;
View Code
6、定義軟件中需要使用的基本變量,包含_flag是否繼續(xù)輸入,_thread當(dāng)前打字的線(xiàn)程,_spend線(xiàn)程暫停的時(shí)間定義代碼如下
1 //定義狀態(tài) 2 bool _flag = true; 3 //定義鍵盤(pán)輸入的速度 4 private int _spend = 800; 5 //定義線(xiàn)程 6 private Thread _t;
7、定義一個(gè)模型 Info 用于下拉框的數(shù)據(jù)源
/// <summary>
/// info下拉框數(shù)據(jù)源
/// </summary>
public class Info
{
public string Name { get; set; }
public string Id { get; set; }
}
8、初始化下拉框,在構(gòu)造函數(shù)中初始化
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public Form1()
{
InitializeComponent();
btnStop.Enabled = false;
//初始化下拉框
IList<Info> infoList = new List<Info>();
Info info1 = new Info() { Id = "300", Name = "快速(200字/分)" };
Info info2 = new Info() { Id = "500", Name = "中速(120字/分)" };
Info info3 = new Info() { Id = "800", Name = "慢速(75字/分)" };
infoList.Add(info1);
infoList.Add(info2);
infoList.Add(info3);
cbSpeend.DataSource = infoList;
cbSpeend.ValueMember = "Id";
cbSpeend.DisplayMember = "Name";
}
9、開(kāi)始按鈕單擊事件,單擊開(kāi)始按鈕后啟動(dòng)線(xiàn)程開(kāi)始自動(dòng)打字。同事禁用開(kāi)始和下拉框
1 /// <summary>
2 /// 開(kāi)始按鈕單擊事件
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void btnStart_Click(object sender, EventArgs e)
7 {
8 _flag = true;
9 btnStart.Enabled = false;
10 cbSpeend.Enabled = false;
11 btnStop.Enabled = true;
12 _spend = int.Parse(cbSpeend.SelectedValue.ToString());
13 //初始化線(xiàn)程
14 _thread = new Thread(KeyBoardStart);
15 _thread.IsBackground = true;
16 _thread.Start();
17 txtInWord.Focus();
18 }
19
20 private void KeyBoardStart()
21 {
22 while (_flag)
23 {
24 try
25 {
26 //點(diǎn)擊A鍵
27 INPUT inDown = new INPUT();
28 inDown.type = INPUT_KEYBOARD;
29 inDown.ki.wVk = (int)Keys.A;
30 SendInput(1, ref inDown, Marshal.SizeOf(inDown));
31 //點(diǎn)擊空格鍵
32 inDown = new INPUT();
33 inDown.type = INPUT_KEYBOARD;
34 inDown.ki.wVk = (int)Keys.Space;
35 SendInput(1, ref inDown, Marshal.SizeOf(inDown));
36 //線(xiàn)程暫停
37 Thread.Sleep(_spend);
38 }
39 catch (Exception ex)
40 {
41 MessageBox.Show(ex.Message);
42 }
43 }
44
45 MessageBox.Show(@"打字結(jié)束");
46 //啟用按鈕開(kāi)始
47 SetBtnEnabled(btnStart, true);
48 //禁用停止按鈕
49 SetBtnEnabled(btnStop, false);
50 //啟用下拉框
51 SetComEnabled(cbSpeend, true);
52 }
View Code
10、開(kāi)始打字線(xiàn)程中使用了委托來(lái)設(shè)置按鈕和下拉框的狀態(tài),這樣可以使線(xiàn)程安全。同事定義設(shè)置按鈕狀態(tài)的安全方法以及設(shè)置下拉框的安全方法。
1 /// <summary>
2 /// 定義委托 設(shè)置按鈕的狀態(tài)
3 /// </summary>
4 /// <param name="btn">按鈕</param>
5 /// <param name="b">false:禁用;true:?jiǎn)⒂?lt;/param>
6 delegate void SetBtnEnabledDel(Button btn, bool b);
7 /// <summary>
8 /// 定義委托 設(shè)置下拉框的狀態(tài)
9 /// </summary>
10 /// <param name="cb">下拉框</param>
11 /// <param name="b">false:禁用;true:?jiǎn)⒂?lt;/param>
12 delegate void SetComEnabledDel(ComboBox cb, bool b);
13
14 /// <summary>
15 /// 設(shè)置下拉框的屬性
16 /// </summary>
17 /// <param name="cb"></param>
18 /// <param name="b"></param>
19 private void SetComEnabled(ComboBox cb, bool b)
20 {
21 if (cb.InvokeRequired)
22 {
23 //在使用用委托調(diào)用自己
24 SetComEnabledDel sbe = SetComEnabled;
25 Invoke(sbe, cb, b);
26 }
27 else
28 {
29 cb.Enabled = b;
30 }
31 }
32
33 /// <summary>
34 /// 設(shè)置按鈕的狀態(tài)
35 /// </summary>
36 /// <param name="btn"></param>
37 /// <param name="b"></param>
38 private void SetBtnEnabled(Button btn, bool b)
39 {
40 if (btn.InvokeRequired)
41 {
42 //在使用用委托調(diào)用自己
43 SetBtnEnabledDel sbe = SetBtnEnabled;
44 Invoke(sbe, btn, b);
45 }
46 else
47 {
48 btn.Enabled = b;
49 }
50 }
View Code
11、定義停止按鈕事件,需要將,輸入狀態(tài)改為false。關(guān)閉窗體的時(shí)候清理窗體的子線(xiàn)程。
1 /// <summary>
2 /// 停止按鈕事件
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void btnStop_Click(object sender, EventArgs e)
7 {
8 _flag = false;
9 }
10
11 /// <summary>
12 /// 關(guān)閉窗體事件
13 /// </summary>
14 /// <param name="sender"></param>
15 /// <param name="e"></param>
16 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
17 {
18 try
19 {
20 if (_thread != null)
21 {
22 //清除線(xiàn)程
23 _thread.DisableComObjectEagerCleanup();
24 }
25 }
26 catch (Exception ex)
27 {
28 MessageBox.Show(ex.Message);
29 }
30 }
View Code
功能截圖:
1、首先運(yùn)行程序
2、將輸入發(fā)切換到中文
3、選擇速度開(kāi)始自動(dòng)打字
源碼下載地址:
http://pan.baidu.com/s/1i3Ek4b7 百度云盤(pán)
總結(jié)
以上是生活随笔為你收集整理的c# 搜狗拼音输入法,刷输入速度和累计输入的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 移动开发:iphone开发之触摸事件详解
- 下一篇: 在64位系统上注册并使用32位的COM组