日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

c# 搜狗拼音输入法,刷输入速度和累计输入

發布時間:2023/12/13 综合教程 35 生活家
生活随笔 收集整理的這篇文章主要介紹了 c# 搜狗拼音输入法,刷输入速度和累计输入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

事件起因:

  搜狗拼音有幾個稱號(光速超人:要求最快打字速度 200字/m,一代文豪:要求累計輸入字數達200000)一直沒有那么快的速度,就想用.net來實現。

相關技術:

  1、winform基本控件使用

  2、多線程開發

  3、C# Win32api函數調用

核心代碼

  1、在窗體中放入兩個按鈕 分別名稱為:開始(name:btnStart) 停止(btnStop)

  2、添加一個下拉框為cbSpeend 輸入速度下拉選項

  3、添加文本框命名為txtInWord

  4、后臺需要引用命名空間

1 using System.Runtime.InteropServices;

  5、導入鍵盤輸入方法SendInput,該方法包含了對鍵盤,鼠標,硬件輸入的底層方法。定義代碼如下

 1  //導入SendInput方法
 2         [DllImport("user32.dll")]
 3         public static extern UInt32 SendInput(UInt32 nInputs, ref INPUT pInputs, int cbSize);
 4 
 5         //輸入結構體
 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         //鼠標輸入結構體
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         //鍵盤輸入結構體
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         //硬件輸入結構體
43         [StructLayout(LayoutKind.Sequential)]
44         public struct HARDWAREINPUT
45         {
46             public Int32 uMsg;
47             public Int16 wParamL;
48             public Int16 wParamH;
49         }
50         //鍵盤輸入
51         public const int INPUT_KEYBOARD = 1;

View Code

  6、定義軟件中需要使用的基本變量,包含_flag是否繼續輸入,_thread當前打字的線程,_spend線程暫停的時間定義代碼如下

1         //定義狀態
2         bool _flag = true;
3         //定義鍵盤輸入的速度
4         private int _spend = 800;
5         //定義線程
6         private Thread _t;    

  7、定義一個模型 Info 用于下拉框的數據源

    /// <summary>
    /// info下拉框數據源
    /// </summary>
    public class Info
    {
        public string Name { get; set; }

        public string Id { get; set; }
    }

  8、初始化下拉框,在構造函數中初始化

        /// <summary>
        /// 構造函數
        /// </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、開始按鈕單擊事件,單擊開始按鈕后啟動線程開始自動打字。同事禁用開始和下拉框

 1  /// <summary>
 2         /// 開始按鈕單擊事件
 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             //初始化線程
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                     //點擊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                     //點擊空格鍵
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                     //線程暫停
37                     Thread.Sleep(_spend);
38                 }
39                 catch (Exception ex)
40                 {
41                     MessageBox.Show(ex.Message);
42                 }
43             }
44 
45             MessageBox.Show(@"打字結束");
46             //啟用按鈕開始
47             SetBtnEnabled(btnStart, true);
48             //禁用停止按鈕
49             SetBtnEnabled(btnStop, false);
50             //啟用下拉框
51             SetComEnabled(cbSpeend, true);
52         }

View Code

  10、開始打字線程中使用了委托來設置按鈕和下拉框的狀態,這樣可以使線程安全。同事定義設置按鈕狀態的安全方法以及設置下拉框的安全方法。

 1 /// <summary>
 2         /// 定義委托 設置按鈕的狀態
 3         /// </summary> 
 4         /// <param name="btn">按鈕</param>
 5         /// <param name="b">false:禁用;true:啟用</param>
 6         delegate void SetBtnEnabledDel(Button btn, bool b);
 7         /// <summary>
 8         /// 定義委托 設置下拉框的狀態
 9         /// </summary>
10         /// <param name="cb">下拉框</param>
11         /// <param name="b">false:禁用;true:啟用</param>
12         delegate void SetComEnabledDel(ComboBox cb, bool b);
13 
14         /// <summary>
15         /// 設置下拉框的屬性
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                 //在使用用委托調用自己
24                 SetComEnabledDel sbe = SetComEnabled;
25                 Invoke(sbe, cb, b);
26             }
27             else
28             {
29                 cb.Enabled = b;
30             }
31         }
32 
33         /// <summary>
34         /// 設置按鈕的狀態
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                 //在使用用委托調用自己
43                 SetBtnEnabledDel sbe = SetBtnEnabled;
44                 Invoke(sbe, btn, b);
45             }
46             else
47             {
48                 btn.Enabled = b;
49             }
50         }

View Code

  11、定義停止按鈕事件,需要將,輸入狀態改為false。關閉窗體的時候清理窗體的子線程。

 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         /// 關閉窗體事件
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                     //清除線程
23                     _thread.DisableComObjectEagerCleanup();
24                 }
25             }
26             catch (Exception ex)
27             {
28                 MessageBox.Show(ex.Message);
29             }
30         }

View Code

功能截圖:

  1、首先運行程序

  2、將輸入發切換到中文

  3、選擇速度開始自動打字  

源碼下載地址:

  http://pan.baidu.com/s/1i3Ek4b7 百度云盤

總結

以上是生活随笔為你收集整理的c# 搜狗拼音输入法,刷输入速度和累计输入的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。