串口线转CH9329,HID控制电脑方案(适用于无法上外网的电脑)

这篇具有很好参考价值的文章主要介绍了串口线转CH9329,HID控制电脑方案(适用于无法上外网的电脑)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1、使用C#语言编写,可自行选择com口,键盘鼠标可以分别控制。

2、需要使用一根串口转HID数据线,某宝可买。

3、基本思路,使用键盘钩子监测键盘输入。使用timer获取鼠标位置,当然也可以用鼠标钩子,这里使用的是timer空间获取鼠标位置,所以存在一个分辨率或者说是延迟的问题。但基本的使用没问题。

4、需要结合CH9329芯片的通信协议来编写,以发送正确的键盘和鼠标操作编码。具体的芯片通信协议网上可查。

(1)鼠标数据包

ch9329,电脑,c#,windows

ch9329,电脑,c#,windows

 (2)键盘数据包

ch9329,电脑,c#,windows

ch9329,电脑,c#,windows

(3)这里只是实现基本的键盘和鼠标操作,其他操作自行查看数据手册开发编写。

5、完整代码

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Button = System.Windows.Forms.Button;
using System.Threading;

namespace WindowsFormsApp1
{

    public partial class Form1 : Form
    {
        int ControlState = 0;                                                        //判断当前空置状态,0表示没有启用控制
        int ControlStateMouse = 0;
        int MousClickTimes = 0;
        int LeftMouseIf = 0;
        string[] CMD_GET_INFO = {"57","AB","00","01","00","03"}; //获取键盘状态的命令码
        public void GET_Information(string[] CMD)
        {
            if (serialPort1.IsOpen)      //如果串口开启
            {
                //串口发送数据的方式1
                byte[] buffer = new byte[1];
                string convertdata;
                /*发送按下键盘信号*/
                convertdata = CMD[0];
                buffer[0] = Convert.ToByte(convertdata, 16);
                serialPort1.Write(buffer, 0, 1);
                convertdata = CMD[1];
                buffer[0] = Convert.ToByte(convertdata, 16);
                serialPort1.Write(buffer, 0, 1);
                convertdata = CMD[2];
                buffer[0] = Convert.ToByte(convertdata, 16);
                serialPort1.Write(buffer, 0, 1);
                convertdata = CMD[3];
                buffer[0] = Convert.ToByte(convertdata, 16);
                serialPort1.Write(buffer, 0, 1);
                convertdata = CMD[4];
                buffer[0] = Convert.ToByte(convertdata, 16);
                serialPort1.Write(buffer, 0, 1);
                convertdata = CMD[5];
                buffer[0] = Convert.ToByte(convertdata, 16);
                serialPort1.Write(buffer, 0, 1);
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
}
        int 功能键 = 0x00;
        string[] KeysNames = {"鼠标左键","鼠标右键","Ctrl-Break键","鼠标中键", "*", "*", "*", "Backspace键", "Tab键", "*", "*", "Clear键","Enter键","*", "*", "Shift键","Ctrl键","Alt键",
            "Pause键","Caps Lock键","*", "*","*", "*", "*", "*", "Esc键","*", "*", "*", "*", "Space键","Page Up键", "Page Down键","End键","Home键","←键","↑键","→键","↓键","Select键",
            "Print键","Execute键", "Print Screen键","Ins键", "Del键", "Help键", "0键","1键","2键","3键","4键","5键","6键","7键","8键","9键","*", "*", "*", "*","*", "*", "*", "A键","B键","C键",
            "D键","E键","F键","G键","H键","I键","J键","K键","L键","M键","N键","O键","P键","Q键","R键","S键","T键","U键","V键","W键","X键","Y键","Z键","左Windows键", "右Windows键",
            "应用程序键", "*", "休眠键","数字键盘0键",  "数字键盘1键",  "数字键盘2键",  "数字键盘3键","数字键盘4键","数字键盘5键",  "数字键盘6键",  "数字键盘7键",  "数字键盘8键",
            "数字键盘9键","乘号键",  "加号键",  "分割键",  "减号键",  "小数点键", "除号键",  "F1键", "F2键",  "F3键","F4键",  "F5键",  "F6键",  "F7键",  "F8键",  "F9键",  "F10键", "F11键",
            "F12键","F13键", "F14键","F15键", "F16键", "F17键", "F18键", "F19键", "F20键", "F21键", "F22键", "F23键","F24键", "*","*", "*", "*", "*", "*", "*", "*", "Num Lock键",
            "Scroll Lock键", "*","*", "*", "*","*", "*", "*", "*", "*", "*", "*", "*", "*","*", "左Shift键","右Shift键","左Ctrl键","右Ctrl键", "左Alt键","右Alt键","*","*","*","*","搜索", "收藏", "浏览器",
            "静音", "音量减","音量加","*", "*","*", "停止", "邮件", "*","*","*","*","*",";:", "=+", ",<", "_-", ".>", "/?", "`~", "*","*","*","*","*","*", "*","*","*","*","*","*","*","*","*","*","*","*","*","*",
            "*","*","*","*","*","*","[{","\\|", "]}", "\'\""};//按键名称对应键盘码值
        /***************************************************/
        static int[] Key_A_Z = { 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D };//a-z编码
        int key_a = Key_A_Z[0]; int key_b = Key_A_Z[1]; int key_c = Key_A_Z[2]; int key_d = Key_A_Z[3]; int key_e = Key_A_Z[4]; int key_f = Key_A_Z[5]; int key_g = Key_A_Z[6];
        int key_h = Key_A_Z[7]; int key_i = Key_A_Z[8]; int key_j = Key_A_Z[9]; int key_k = Key_A_Z[10]; int key_l = Key_A_Z[11]; int key_m = Key_A_Z[12]; int key_n = Key_A_Z[13];
        int key_o = Key_A_Z[14]; int key_p = Key_A_Z[15]; int key_q = Key_A_Z[16]; int key_r = Key_A_Z[17]; int key_s = Key_A_Z[18]; int key_t = Key_A_Z[19]; int key_u = Key_A_Z[20];
        int key_v = Key_A_Z[21]; int key_w = Key_A_Z[22]; int key_x = Key_A_Z[23]; int key_y = Key_A_Z[24]; int key_z = Key_A_Z[25]; //将键盘码重新命名
        /***************************************************/
        static int[] Key_0_9 = { 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 };                                                              //主键盘0-9
        int key__1 = Key_0_9[0]; int key__2 = Key_0_9[1]; int key__3 = Key_0_9[2]; int key__4 = Key_0_9[3]; int key__5 = Key_0_9[4]; int key__6 = Key_0_9[5]; int key__7 = Key_0_9[6];
        int key__8 = Key_0_9[7]; int key__9 = Key_0_9[8]; int key__0 = Key_0_9[9];                                                                             //将主键盘数字码重新命名,注意是2个下划线
        /***************************************************/
        static int[] Controlbyte = { 0x80, 0x40, 0x20, 0x01, 0x08, 0x04, 0x02, 0x01, 0x00 };                                                                        //控制码
        //八位控制码分别为:右win,右alt,右shift,右ctrl,左win,左alt,左shift,左ctrl,无控制,此处控制码只包含按下一个控制键的情况,不包含多个控制键的复杂情况
        int key__RightWin = Controlbyte[0]; int key__RightAlt = Controlbyte[1]; int key__RightShift = Controlbyte[2]; int key__RightControl = Controlbyte[3];
        int key__LeftWin = Controlbyte[4]; int key__LeftAlt = Controlbyte[5]; int key__LeftShift = Controlbyte[6]; int key__LeftControl = Controlbyte[7];
        int key__NoControl = Controlbyte[8];                                                                                                                                           //将控制码重新命名,方便实用
        /************************************************************************************************************/
        static int[] 标点 = { 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x33, 0x34, 0x36, 0x37, 0x38 };//代码依次为:0-  1=  2[   3]   4\   5;    6'   7,   8.   9/
        //直接看上面印的注释序号吧,懒得起名字了
        /*****************************功能按键**********************************************/
        //F1-F12,TAB,大小写,左shift,左ctrl,左win,左alt,右alt,右win,右ctrl,右shift,主enter,数字区enter,菜单键,空格键,退格键
        static int[] FunctionKey = {0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,0x40,0x41,0x42,0x43,0x44,0x45, 0x2B,0x39,
        0xE1,0xE0,0xE3,0xE2,0xE6,0xE7,0xE4,0xE5,0x28,0x58,0x65,0x2C,0x2A,0x52,0x51,0x50,0x4F};
        int keyf1 = FunctionKey[0]; int keyf2 = FunctionKey[1]; int keyf3 = FunctionKey[2]; int keyf4 = FunctionKey[3]; int keyf5 = FunctionKey[4]; int keyf6 = FunctionKey[5];
        int keyf7 = FunctionKey[6]; int keyf8 = FunctionKey[7]; int keyf9 = FunctionKey[8]; int keyf10 = FunctionKey[9]; int keyf11 = FunctionKey[10]; int keyf12 = FunctionKey[11];
        int keyTab = FunctionKey[12]; int keyCap = FunctionKey[13]; int Lshift = FunctionKey[14]; int Lctrl = FunctionKey[15]; int Lwin = FunctionKey[16]; int Lalt = FunctionKey[17];
        int Ralt = FunctionKey[18]; int Rwin = FunctionKey[19]; int Rctrl = FunctionKey[20]; int Rshift = FunctionKey[21]; int Lenter = FunctionKey[22]; int Renter = FunctionKey[23];
        int App = FunctionKey[24]; int key_Space = FunctionKey[25]; int key_Backspace = FunctionKey[26]; int keyUp = FunctionKey[27]; int keyDown = FunctionKey[28];
        int keyLeft = FunctionKey[29]; int keyRight = FunctionKey[30]; //功能按键重新起名字,方便使用
        /***************************************功能按键**********************************************/
        /***************************************小键盘数字键**********************************************/
        static int[] NumberKey = { 0x59,0x5a,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,0x61,0x62,0x53};
        int key_Number1 = NumberKey[0];int key_Number2 = NumberKey[1];int key_Number3 = NumberKey[2];int key_Number4 = NumberKey[3];
        int key_Number5 = NumberKey[4];int key_Number6 = NumberKey[5];int key_Number7 = NumberKey[6];int key_Number8 = NumberKey[7];
        int key_Number9 = NumberKey[8];int key_Number0 = NumberKey[9]; int key_Num_Lock = NumberKey[10];//重新命名,方便使用
        /***************************************小键盘数字键**********************************************/
        static int[] MouseCode = {0x57,0xAB,0x00,0x04,0x07,0x02};   //前几个字节是固定的,存到数组里
        static int[] MouseKey = {0x01,0x02,0x04,0x00};                      //储存鼠标按键操作,分别为点击左键,点击右键,点击中键,同时点击的情况不开发,很少用到
        int LeftClick = MouseKey[0];int RightClick = MouseKey[1];int MidClick = MouseKey[2];int FreeMouseKey = MouseKey[3];
        string 校验码(int ControlByte, int KeyByte)                    //计算键盘数据累加和
        {
            //两个参数分别为控制码和键盘码,虽说键盘码可以同时发送6个,但是就用一个就够了。
            return (0x57 + 0xAB + 0x02 + 0x08 + ControlByte + KeyByte).ToString("X6").Substring(4, 2);
        }
        string 鼠标校验码(int Keycode, int Xlocation1,int Xlocation2,int Ylocation1,int Ylocation2,int RollNum)             //计算鼠标数据累加和
        {//参数分别是,按键代码,X坐标1字节,X坐标2字节,Y坐标1字节,Y坐标2字节,滚轮滚动齿数
            return (0x57 + 0xAB + 0x04 + 0x07 + 0x02 + Keycode + Xlocation1 + Xlocation2 + Ylocation1 + Ylocation2 + RollNum).ToString("X6").Substring(4, 2);
        }
        public int Num(char a)          //1位字符16进制数字转10进制
        {
            if (a == '0'){return 0;}
            else if (a == '1'){return 1;}else if (a == '2'){return 2;}else if (a == '3'){return 3; }
            else if (a == '4'){return 4;}else if (a == '5') {return 5;}else if (a == '6'){return 6;}else if (a == '7'){return 7; }else if (a == '8'){return 8;}else if (a == '9'){ return 9;}
            else if (((a == 'a') || (a == 'A'))){return 10; }else if ((a == 'b') || (a == 'B')) {return 11;}else if ((a == 'c') || (a == 'C')){return 12;}else if ((a == 'd') || (a == 'D')){return 13;}
            else if ((a == 'e') || (a == 'E')){return 14; }else {return 15;}
        }
        public int HexToBin(string a) //2位16进制转10进制
        {
            return Num(a[0]) * 16 + Num(a[1]);
        }
        public void SendMouseData(int Keycode, int Xlocation1, int Xlocation2, int Ylocation1, int Ylocation2, int RollNum)
        {//参数说明:鼠标按键码,x坐标1,x坐标2,y坐标1,y坐标2,滚轮齿数
            if (ControlStateMouse == 1)                                                     //判断是否打开控制
            {
                string Hex_Keycode = Keycode.ToString("X6").Substring(4, 2);          //都转换成16进制字符串,方便串口传输
                string Hex_Xlocation1 = Xlocation1.ToString("X6").Substring(4, 2);
                string Hex_Xlocation2 = Xlocation2.ToString("X6").Substring(4, 2);
                string Hex_Ylocation1 = Ylocation1.ToString("X6").Substring(4, 2);
                string Hex_Ylocation2 = Ylocation2.ToString("X6").Substring(4, 2);
                string Hex_RollNum = RollNum.ToString("X6").Substring(4, 2);
                if (serialPort1.IsOpen)      //如果串口开启
                {
                    //串口发送数据的方式1
                    byte[] buffer = new byte[1];
                    string convertdata;
                    /*发送按下键盘信号*/
                    convertdata = MouseCode[0].ToString("X6").Substring(4,2);
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = MouseCode[1].ToString("X6").Substring(4, 2);
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = MouseCode[2].ToString("X6").Substring(4, 2);
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = MouseCode[3].ToString("X6").Substring(4, 2);
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = MouseCode[4].ToString("X6").Substring(4, 2);
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = MouseCode[5].ToString("X6").Substring(4, 2);
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = Hex_Keycode;
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = Hex_Xlocation1;
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = Hex_Xlocation2;
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = Hex_Ylocation1;
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = Hex_Ylocation2;
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = Hex_RollNum;
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                    convertdata = 鼠标校验码(Keycode, Xlocation1, Xlocation2, Ylocation1, Ylocation2, RollNum);
                    buffer[0] = Convert.ToByte(convertdata, 16);
                    serialPort1.Write(buffer, 0, 1);
                }
                else
                {
                    MessageBox.Show("串口未打开");
                }
            }
        }
        public void SendData(int Control, int KeyByte)    //发送数据的函数,参数分别为控制命令和键盘键位数据,具体控制命令见芯片通信协议
        {
            if (ControlState == 1)                      //判断是否打开控制  Convert.ToString(KeyByte16)
            {
                ControlByte.Text = Convert.ToString(Control, 16);       //控制代码也要转成16进制字符
                键盘码.Text = Convert.ToString(KeyByte, 16);                //键盘码要转换为16进制,直接弄text他会是十进制
                SumByte.Text = 校验码(Control, KeyByte);
                if (serialPort1.IsOpen)      //如果串口开启
                {
                    if ((send1.Text.ToString() != "") && ((send2.Text.ToString() != "")) && ((send3.Text.ToString() != "")) && ((send4.Text.ToString() != "")) && ((send5.Text.ToString() != ""))
                        && ((ControlByte.Text.ToString() != "")) && ((send7.Text.ToString() != "")) && ((键盘码.Text.ToString() != "")) && ((send9.Text.ToString() != "")) && ((send10.Text.ToString() != ""))
                        && ((send11.Text.ToString() != "")) && ((send12.Text.ToString() != "")) && ((send13.Text.ToString() != "")) && ((SumByte.Text.ToString() != ""))) //如果框内不为空则
                    {
                        //串口发送数据的方式1
                        byte[] buffer = new byte[1];
                        string convertdata;
                        /*发送按下键盘信号*/
                        convertdata = send1.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = send2.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = send3.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = send4.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = send5.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = ControlByte.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = send7.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = 键盘码.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = send9.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = send10.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = send11.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = send12.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = send13.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        convertdata = SumByte.Text;
                        buffer[0] = Convert.ToByte(convertdata, 16);
                        serialPort1.Write(buffer, 0, 1);
                        /*抬起键盘*/
                    }
                    else
                    {
                        MessageBox.Show("发送框没有数据");
                    }
                }
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        public Form1()   //初始化一些东西
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            comboBox1.SelectedText = "中";
            timer1.Enabled = false;
            button53.Text = "    ~\n`"; key_1.Text = "    !\n1"; key_2.Text = "    @\n2"; key_3.Text = "    #\n3"; key_4.Text = "    $\n4"; key_5.Text = "    %\n5"; key_6.Text = "    ^\n6";
            key_7.Text = "    &&" + "\n" + "7"; key_8.Text = "    *\n8"; key_9.Text = "    (\n9"; key_0.Text = "    )\n0"; button42.Text = "    _\n-"; button41.Text = "    +\n=";
            label6.Text = "串口:关"; label9.Text = "键盘:关"; send1.Text = "57"; send2.Text = "AB"; send3.Text = "00"; send4.Text = "02"; send5.Text = "08"; send7.Text = "00";
            send9.Text = "00"; send10.Text = "00"; send11.Text = "00"; send12.Text = "00"; send13.Text = "00"; Num_Lock.Text = "Num\nLock"; button2.Text = "启 用\n" + "鼠 标";
            button5.Text = "停 用\n" + "鼠 标"; label1.Text = "鼠标:关";
            //初始化button显示文字
            /*使textbox不可选中,省的按TAB的时候总是乱串*/
            send1.TabStop = false; send2.TabStop = false; send3.TabStop = false; send4.TabStop = false; send5.TabStop = false;
            ControlByte.TabStop = false; send7.TabStop = false; 键盘码.TabStop = false; send9.TabStop = false; send10.TabStop = false;
            send11.TabStop = false; send12.TabStop = false; send13.TabStop = false; SumByte.TabStop = false;
            /*******************************************/
            label3.Text = "";
            send1.MaxLength = 2; send2.MaxLength = 2; send3.MaxLength = 2; send4.MaxLength = 2; send5.MaxLength = 2; ControlByte.MaxLength = 2; send7.MaxLength = 2;
            键盘码.MaxLength = 2; send9.MaxLength = 2; send10.MaxLength = 2; send11.MaxLength = 2; send12.MaxLength = 2; send13.MaxLength = 2; SumByte.MaxLength = 2;
            ControlByte.Text = "00";
            键盘码.Text = "04"; send9.Text = "00"; send10.Text = "00"; send11.Text = "00"; send12.Text = "00"; send13.Text = "00"; SumByte.Text = "10";
            send1.ReadOnly = true; send2.ReadOnly = true; send3.ReadOnly = true; send4.ReadOnly = true; send5.ReadOnly = true; ControlByte.ReadOnly = true;
            send7.ReadOnly = true; 键盘码.ReadOnly = true; send9.ReadOnly = true; send10.ReadOnly = true; send11.ReadOnly = true; send12.ReadOnly = true;
            send13.ReadOnly = true; SumByte.ReadOnly = true; 端口选择.DropDownStyle = ComboBoxStyle.DropDownList;
        }
        //安装钩子
        [DllImport("user32.dll")]//windows提供的界面UI和消息机制
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hlnstance, int Threadld);
        //Theradld:表示监视的线程
        //卸载钩子
        [DllImport("user32.dll")]
        public static extern int UnhookWindowsHookEx(int idHook);
        //当前操作系统有很多软件都在监视键盘,所以在该程序捕键盘操作以后进行传递,否则其他程序无法捕获键盘操作
        //传递下一个钩子
        //上一个捕获之后准备捕获下一个
        [DllImport("user32.dll")]
        public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);
        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);//
        HookProc KeyBoardHookProcedure, mouseHookProcedure;//定义键盘和鼠标
        int hHook;
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        [StructLayout(LayoutKind.Sequential)]
        class KeyBoardHookStruct
        {
            public int vkCode;      //1-254虚拟键码
            public int dwExtrainfo;
            public int time;
        }
        //串口数据接收事件
        //取消获得焦点
        public void UpKeys()  //抬起按键函数
        {
            if (功能键 == 0x00)
            {
                SendData(0x00, 0x00);
            }
            else
            {
                SendData(功能键, 0x00);
            }
        }
        public static void SelectButton(Button button)
        {
            MethodInfo methodinfo = button.GetType().GetMethod("SetStyle", BindingFlags.NonPublic | BindingFlags.Instance);
            methodinfo.Invoke(button, BindingFlags.NonPublic, null, new object[] { ControlStyles.Selectable, false }, null);
        }
        //打开串口
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = 端口选择.SelectedItem.ToString();
                serialPort1.BaudRate = Convert.ToInt32(label3.Text, 10);   //十进制数据转换
                serialPort1.Open();
                GET_Information(CMD_GET_INFO);                                  //获取对方电脑键盘状态
                按钮_打开串口.Enabled = false;                                         //打开串口按钮不可用
                按钮_停用控制.Enabled = true;                                          
                label6.Text = "串口:开";
                端口选择.Enabled = false;                                                 //让combox失去焦点,以方便tab的发送
            }
            catch
            {
                MessageBox.Show("端口选择错误,请检查串口!\n先点击初始化再开启串口!", "错误");
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            SelectButton(按钮_打开串口); SelectButton(按钮_停用控制); SelectButton(key_0); SelectButton(按钮_初始化); SelectButton(key_Q);
            SelectButton(key_W); SelectButton(key_E); SelectButton(key_R); SelectButton(key_T); SelectButton(key_Y); SelectButton(button1); SelectButton(button2);
            SelectButton(key_T); SelectButton(key_Y); SelectButton(key_U); SelectButton(key_I); SelectButton(key_O); SelectButton(button5);
            SelectButton(key_P); SelectButton(key_A); SelectButton(key_S); SelectButton(key_D); SelectButton(key_F);
            SelectButton(key_G); SelectButton(key_H); SelectButton(key_J); SelectButton(key_K); SelectButton(key_L);
            SelectButton(key_Z); SelectButton(key_X); SelectButton(key_C); SelectButton(key_V); SelectButton(key_B);
            SelectButton(key_N); SelectButton(key_M); SelectButton(key_9); SelectButton(key_8); SelectButton(key_7);
            SelectButton(key_6); SelectButton(key_5); SelectButton(key_4); SelectButton(key_3); SelectButton(key_2);
            SelectButton(key_1); SelectButton(key_Back); SelectButton(button41); SelectButton(button42); SelectButton(key_Right方括号大括号);//key_Num1
            SelectButton(Key_Left方括号大括号); SelectButton(反斜杠); SelectButton(key_Enter); SelectButton(单引号双引号); SelectButton(分号冒号);
            SelectButton(key_RightShift); SelectButton(斜杠); SelectButton(句号Right尖括号); SelectButton(逗号Left尖括号); SelectButton(button53);
            SelectButton(button54); SelectButton(button55); SelectButton(key_LeftShift); SelectButton(key_RightControl); SelectButton(key_App);
            SelectButton(key_RightWin); SelectButton(key_RightAlt); SelectButton(Key_Space); SelectButton(key_LeftAlt); SelectButton(key_LeftWin);SelectButton(key_Right);
            SelectButton(key_LeftControl); SelectButton(按钮_启用控制); SelectButton(key_Num1); SelectButton(key_Num2); SelectButton(key_Num3); SelectButton(key_Num4);
            SelectButton(key_Num5); SelectButton(key_Num6); SelectButton(key_Num7); SelectButton(key_Num8); SelectButton(key_Num9); SelectButton(key_Num0);
            SelectButton(Num_Lock); SelectButton(key_Up); SelectButton(button3); SelectButton(button4); SelectButton(Button设备管理);SelectButton(key_Left);SelectButton(key_Down);
            label3.Text = "9600";//波特率默认值
            /*****************非常重要************************/
            //手动添加事件处理程序(也可以在前面的事件中添加)
            //       serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            //      button4.Enabled = false;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            UnhookWindowsHookEx(hHook);
            label9.Text = "键盘:关";
            按钮_停用控制.Enabled = false;
            按钮_启用控制.Enabled = true;
            ControlState = 0;
        }

        private void button65_Click(object sender, EventArgs e)
        {
            KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);//捕获之后的操作
            hHook = SetWindowsHookEx(13, KeyBoardHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
            //ModuleName:得到当前的模块名字;13是固定参数,表示检测键盘;
            //注册完钩子,返回钩子句柄:hHook
            label9.Text = "键盘:开";
            按钮_启用控制.Enabled = false;
            按钮_停用控制.Enabled = true;
            ControlState = 1;
        }

        private void Button设备管理_Click(object sender, EventArgs e)            //打开设备管理器,以方便查看COM口
        {
            System.Diagnostics.Process.Start("rundll32.exe", @"devmgr.dll DeviceManager_Execute");
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)   //串口收到数据触发
        {
            /*******************************串口接收数据*********************************/
            Thread th = new Thread(Read_Info);       //创建线程
            th.Start();
            /*******************************串口接收数据*********************************/
        }
        public void Read_Info()                                                                                          //读取串口来的数据的函数
        {
            textBox1.Text = "";
            int length = serialPort1.BytesToRead;
            byte[] data = new byte[length];
            try{
                serialPort1.Read(data, 0, length);
            }
            catch
            {
                MessageBox.Show("程序已退出","提示");
            }
            for (int i = 0; i < length; i++)
            {
                string str = Convert.ToString(data[i], 16).ToUpper();
                textBox1.AppendText(str.Length == 1 ? "0" + str + " " : str + " ");                  //将接收到的数据以十六进制显示到文本框内
            }
        }
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                SendMouseData(LeftClick, 0x00, 0x00, 0x00, 0x00, 0x00);
            }
            if (e.Button == MouseButtons.Right)
            {
                SendMouseData(RightClick, 0x00, 0x00, 0x00, 0x00, 0x00);
            }
        }
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                SendMouseData(FreeMouseKey, 0x00, 0x00, 0x00, 0x00, 0x00);
            }
            if (e.Button == MouseButtons.Right)
            {
                SendMouseData(FreeMouseKey, 0x00, 0x00, 0x00, 0x00, 0x00);
            }
        }
        private void timer1_Tick(object sender, EventArgs e)                                                                                                             //设置一个定时器,每100ms获取一次鼠标位置
        {
            Point mousePosition = Control.MousePosition;
            指针坐标.Text = "指针坐标:"+string.Format("X:{0}  Y:{1}", mousePosition.X, mousePosition.Y);                                           //以下代码计算鼠标位置
            string x1 = ((mousePosition.X * 4096 / 1600-200)).ToString("X6").Substring(2, 2);
            string x2 = ((mousePosition.X * 4096 / 1600-200)).ToString("X6").Substring(4, 2);
            X1.Text = "X1:"+ x1; X2.Text = "X2:" +x2; X坐标.Text = "X坐标:   "+(mousePosition.X * 4096 / 1600).ToString("X6");
            string y1 = ((mousePosition.Y * 4096 / 900)-100).ToString("X6").Substring(2, 2);
            string y2 = ((mousePosition.Y * 4096 / 900)-100).ToString("X6").Substring(4, 2);
            Y1.Text = "Y1:" + y1; Y2.Text = "Y2:"+y2; Y坐标.Text = "Y坐标:  " + (mousePosition.Y * 4096 / 900).ToString("X6");
            if(ControlStateMouse == 1)
            {
                SendMouseData(FreeMouseKey, HexToBin(x2), HexToBin(x1), HexToBin(y2), HexToBin(y1), 0x00);
            }
        }
        private void button3_Click_1(object sender, EventArgs e)
        {
            if ((ControlState == 1)||(ControlStateMouse == 1))
            {
                groupBox4.Visible = false;
                label10.Visible = false;
                groupBox1.Visible = false;
                groupBox2.Visible = false;
                groupBox3.Visible = false;
                textBox1.Visible = false;
                指针坐标.Visible = false;
                X坐标.Visible = false;
                X1.Visible = false;
                X2.Visible = false;
                Y坐标.Visible = false;
                Y1.Visible = false;
                Y2.Visible = false;
                button3.Visible = false;
                button4.Visible = false;
                Button设备管理.Visible = false;
            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            groupBox4.Visible = true;
            groupBox1 .Visible =true;
            label10.Visible = true;
            groupBox1.Visible = true;
            groupBox2.Visible = true;
            groupBox3.Visible = true;
            textBox1.Visible = true;
            指针坐标.Visible = true;
            X坐标.Visible = true;
            X1.Visible = true;
            X2.Visible = true;
            Y坐标.Visible = true;
            Y1.Visible = true;
            Y2.Visible = true;
            button3.Visible = true;
            button4.Visible = true;
            Button设备管理.Visible = true;
        }

        private void button1_Click_2(object sender, EventArgs e)
        {
            if(comboBox1.SelectedItem == "高")
            {
                timer1.Interval = 10;
            }
            else if(comboBox1.SelectedItem =="中")
            {
                timer1.Interval = 120;
            }
            else
            {
                timer1.Interval = 200;
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            label1.Text = "鼠标:开";
            timer1.Enabled = true;
            ControlStateMouse = 1;
            button2.Enabled = false;
            button5.Enabled = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            label1.Text = "鼠标:关";
            ControlStateMouse = 0;
            button2.Enabled = true;
            button5.Enabled = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            serialPort1.Close();
        }

        /*****************************************键盘钩子捕获执行程序************************************************/
        int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)//具体要执行的事件,我得知道按下的是什么案件
        {
            KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
            //现在所有信息都保存在kbh中
            label8.Text = "键盘代码:" + kbh.vkCode;
            label7.Text = "键值:" + KeysNames[kbh.vkCode - 1];
            if ((key_W.ForeColor == key_Q.ForeColor)&& (key_W.ForeColor == key_E.ForeColor)) //快捷显示,QWE同时按下。
             {
                groupBox4.Visible = true;
                label10.Visible = true;
                groupBox1.Visible = true;
                groupBox2.Visible = true;
                groupBox3.Visible = true;
                textBox1.Visible = true;
                指针坐标.Visible = true;
                X坐标.Visible = true;
                X1.Visible = true;
                X2.Visible = true;
                Y坐标.Visible = true;
                Y1.Visible = true;
                Y2.Visible = true;
                button3.Visible = true;
                button4.Visible = true;
                Button设备管理.Visible = true;
            }
            /*************************************自定义快捷键************************************/
            if(((key_LeftShift.ForeColor == Color.Red)&& (key_LeftControl.ForeColor == Color.Red))||
                ((key_LeftShift.ForeColor == Color.Red) && (key_RightControl.ForeColor == Color.Red))||
                ((key_RightShift.ForeColor == Color.Red) && (key_LeftControl.ForeColor == Color.Red))||
                ((key_RightShift.ForeColor == Color.Red) && (key_RightControl.ForeColor == Color.Red)))      //输入法切换ctrl+shift
            {
                SendData(0x20, Lctrl);
            }
            /*************************************自定义快捷键************************************///适用于两个功能键同时按下的情况
            if ((kbh.vkCode == 160) && (wParam == 256)) 
            {
                    功能键 = key__LeftShift;
                    key_LeftShift.ForeColor = Color.Red;
            }
            else if ((kbh.vkCode == 160) && (wParam == 257))
            { 
                功能键 = key__NoControl; 
                key_LeftShift.ForeColor = Color.Black; 
                SendData(功能键, Lshift);
            }
            if ((kbh.vkCode == 162) && (wParam == 256))
            { 
                功能键 = key__LeftControl; 
                key_LeftControl.ForeColor = Color.Red; 
            }
            else if ((kbh.vkCode == 162) && (wParam == 257))
            { 
                功能键 = key__NoControl; 
                key_LeftControl.ForeColor = Color.Black; 
                SendData(功能键, Lctrl);
            }

            if ((kbh.vkCode == 91) && (wParam == 256))
            { 
                功能键 = key__LeftWin; 
                key_LeftWin.ForeColor = Color.Red; 
            }
            else if ((kbh.vkCode == 91) && (wParam == 257)) 
            { 
                功能键 = key__NoControl; 
                key_LeftWin.ForeColor = Color.Black; 
                SendData(功能键, Lwin);
            }

            if ((kbh.vkCode == 164) && (wParam == 260))
            { 
                功能键 = key__LeftAlt; 
                key_LeftAlt.ForeColor = Color.Red; 
            }
            else if ((kbh.vkCode == 164) && (wParam == 257)) 
            { 
                功能键 = key__NoControl; 
                key_LeftAlt.ForeColor = Color.Black; 
                SendData(功能键, Lalt); 
            }

            if ((kbh.vkCode == 165) && (wParam == 260))
            { 
                功能键 = key__RightAlt; 
                key_RightAlt.ForeColor = Color.Red;
            }
            else if ((kbh.vkCode == 165) && (wParam == 257)) 
            { 
                功能键 = key__NoControl; 
                key_RightAlt.ForeColor = Color.Black; 
                SendData(功能键, Ralt); 
            }

            if ((kbh.vkCode == 161) && (wParam == 256)) 
            { 
                功能键 = key__RightShift; 
                key_RightShift.ForeColor = Color.Red; 
            }
            else if ((kbh.vkCode == 161) && (wParam == 257))
            { 
                功能键 = key__NoControl;
                key_RightShift.ForeColor = Color.Black; 
                SendData(功能键, Rshift);
            }

            if ((kbh.vkCode == 163) && (wParam == 256)) 
            { 
                功能键 = key__RightControl;
                key_RightControl.ForeColor = Color.Red;
            }
            else if ((kbh.vkCode == 163) && (wParam == 257)) 
            { 
                功能键 = key__NoControl; 
                key_RightControl.ForeColor = Color.Black; 
                SendData(功能键, Rctrl); 
            }

            if ((kbh.vkCode == 92) && (wParam == 256))
            { 
                功能键 = key__RightWin; 
                key_RightWin.ForeColor = Color.Red;
            }
            else if ((kbh.vkCode == 92) && (wParam == 257)) 
            { 功能键 = key__NoControl; 
                key_RightWin.ForeColor = Color.Black; 
                SendData(功能键, Rwin); 
            }
            /***********************单独按下功能键*************************/
            if ((kbh.vkCode == 112) && (wParam == 256)) { SendData(功能键, keyf1); }
            if ((kbh.vkCode == 112) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 113) && (wParam == 256)) { SendData(功能键, keyf2); }
            if ((kbh.vkCode == 113) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 114) && (wParam == 256)) { SendData(功能键, keyf3); }
            if ((kbh.vkCode == 114) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 115) && (wParam == 256)) { SendData(功能键, keyf4); }
            if ((kbh.vkCode == 115) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 116) && (wParam == 256)) { SendData(功能键, keyf5); }
            if ((kbh.vkCode == 116) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 117) && (wParam == 256)) { SendData(功能键, keyf6); }
            if ((kbh.vkCode == 117) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 118) && (wParam == 256)) { SendData(功能键, keyf7); }
            if ((kbh.vkCode == 118) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 119) && (wParam == 256)) { SendData(功能键, keyf8); }
            if ((kbh.vkCode == 119) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 120) && (wParam == 256)) { SendData(功能键, keyf9); }
            if ((kbh.vkCode == 120) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 121) && (wParam == 256)) { SendData(功能键, keyf10); }
            if ((kbh.vkCode == 121) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 122) && (wParam == 256)) { SendData(功能键, keyf11); }
            if ((kbh.vkCode == 122) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 123) && (wParam == 256)) { SendData(功能键, keyf12); }
            if ((kbh.vkCode == 123) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 9) && (wParam == 256)) { SendData(功能键, keyTab); button54.ForeColor = Color.Red; }
            if ((kbh.vkCode == 9) && (wParam == 257)) { UpKeys(); button54.ForeColor = Color.Black; }
            if ((kbh.vkCode == 20) && (wParam == 256)) { SendData(功能键, keyCap); button55.ForeColor = Color.Red; }
            if ((kbh.vkCode == 20) && (wParam == 257)) { UpKeys(); button55.ForeColor = Color.Black; }
            //    if ((kbh.vkCode == 160) && (wParam == 256)) { SendData(功能键, Lshift); }if ((kbh.vkCode == 160) && (wParam == 257)) { UpKeys(); }
            //    if ((kbh.vkCode == 162) && (wParam == 256)) { SendData(功能键, Lctrl); }if ((kbh.vkCode == 162) && (wParam == 257)) { UpKeys(); }
            //    if ((kbh.vkCode == 91) && (wParam == 256)) { SendData(功能键, Lwin); }if ((kbh.vkCode == 91) && (wParam == 257)) { UpKeys(); }
            //    if ((kbh.vkCode == 164) && (wParam == 260)) { SendData(功能键, Lalt); }if ((kbh.vkCode == 164) && (wParam == 257)) { UpKeys(); }
            //    if ((kbh.vkCode == 165) && (wParam == 260)) { SendData(功能键, Ralt); }if ((kbh.vkCode == 165) && (wParam == 257)) { UpKeys(); }
            //    if ((kbh.vkCode == 92) && (wParam == 256)) { SendData(功能键, Rwin); }if ((kbh.vkCode == 92) && (wParam == 257)) { UpKeys(); }
            //    if ((kbh.vkCode == 163) && (wParam == 256)) { SendData(功能键, Rctrl); }if ((kbh.vkCode == 163) && (wParam == 257)) { UpKeys(); }
            //    if ((kbh.vkCode == 161) && (wParam == 256)) { SendData(功能键, Rshift); }if ((kbh.vkCode == 161) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 13) && (wParam == 256)) { SendData(功能键, Lenter); key_Enter.ForeColor = Color.Red; }
            if ((kbh.vkCode == 13) && (wParam == 257)) { UpKeys(); key_Enter.ForeColor = Color.Black; }
            if ((kbh.vkCode == 93) && (wParam == 256)) { SendData(功能键, App); key_App.ForeColor = Color.Red; }
            if ((kbh.vkCode == 93) && (wParam == 257)) { UpKeys(); key_App.ForeColor = Color.Black; }
            if ((kbh.vkCode == 32) && (wParam == 256)) { SendData(功能键, key_Space); Key_Space.BackColor = Color.Red; }
            if ((kbh.vkCode == 32) && (wParam == 257)) { UpKeys(); Key_Space.BackColor = Color.LightGray; }
            if ((kbh.vkCode == 8) && (wParam == 256)) { SendData(功能键, key_Backspace); key_Back.ForeColor = Color.Red; }
            if ((kbh.vkCode == 8) && (wParam == 257)) { UpKeys(); key_Back.ForeColor = Color.Black; }
            if ((kbh.vkCode == 37) && (wParam == 256)) { SendData(功能键, keyLeft); key_Left.ForeColor = Color.Red; }
            if ((kbh.vkCode == 37) && (wParam == 257)) { UpKeys(); key_Left.ForeColor = Color.Black; }
            if ((kbh.vkCode == 38) && (wParam == 256)) { SendData(功能键, keyUp); key_Up.ForeColor = Color.Red; }
            if ((kbh.vkCode == 38) && (wParam == 257)) { UpKeys(); key_Up.ForeColor = Color.Black; }
            if ((kbh.vkCode == 39) && (wParam == 256)) { SendData(功能键, keyRight); key_Right.ForeColor = Color.Red; }
            if ((kbh.vkCode == 39) && (wParam == 257)) { UpKeys(); key_Right.ForeColor = Color.Black; }
            if ((kbh.vkCode == 40) && (wParam == 256)) { SendData(功能键, keyDown); key_Down.ForeColor = Color.Red; }
            if ((kbh.vkCode == 40) && (wParam == 257)) { UpKeys(); key_Down.ForeColor = Color.Black; }
            /***********************单独按下功能键*************************/
            /***********************单独按下按键A-Z*************************/
            if ((kbh.vkCode == 65) && (wParam == 256)) { SendData(功能键, key_a); }
            if ((kbh.vkCode == 65) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 66) && (wParam == 256)) { SendData(功能键, key_b); }
            if ((kbh.vkCode == 66) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 67) && (wParam == 256)) { SendData(功能键, key_c); }
            if ((kbh.vkCode == 67) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 68) && (wParam == 256)) { SendData(功能键, key_d); }
            if ((kbh.vkCode == 68) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 69) && (wParam == 256)) { SendData(功能键, key_e); key_E.ForeColor = Color.Red; }
            if ((kbh.vkCode == 69) && (wParam == 257)) { UpKeys(); key_E.ForeColor = Color.Black; }
            if ((kbh.vkCode == 70) && (wParam == 256)) { SendData(功能键, key_f); }
            if ((kbh.vkCode == 70) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 71) && (wParam == 256)) { SendData(功能键, key_g); }
            if ((kbh.vkCode == 71) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 72) && (wParam == 256)) { SendData(功能键, key_h); }
            if ((kbh.vkCode == 72) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 73) && (wParam == 256)) { SendData(功能键, key_i); }
            if ((kbh.vkCode == 73) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 74) && (wParam == 256)) { SendData(功能键, key_j); }
            if ((kbh.vkCode == 74) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 75) && (wParam == 256)) { SendData(功能键, key_k); }
            if ((kbh.vkCode == 75) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 76) && (wParam == 256)) { SendData(功能键, key_l); }
            if ((kbh.vkCode == 76) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 77) && (wParam == 256)) { SendData(功能键, key_m); }
            if ((kbh.vkCode == 77) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 78) && (wParam == 256)) { SendData(功能键, key_n); }
            if ((kbh.vkCode == 78) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 79) && (wParam == 256)) { SendData(功能键, key_o); }
            if ((kbh.vkCode == 79) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 80) && (wParam == 256)) { SendData(功能键, key_p); }
            if ((kbh.vkCode == 80) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 81) && (wParam == 256)) { SendData(功能键, key_q); key_Q.ForeColor = Color.Red; }
            if ((kbh.vkCode == 81) && (wParam == 257)) { UpKeys(); key_Q.ForeColor = Color.Black; }
            if ((kbh.vkCode == 82) && (wParam == 256)) { SendData(功能键, key_r); }
            if ((kbh.vkCode == 82) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 83) && (wParam == 256)) { SendData(功能键, key_s); }
            if ((kbh.vkCode == 83) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 84) && (wParam == 256)) { SendData(功能键, key_t); }
            if ((kbh.vkCode == 84) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 85) && (wParam == 256)) { SendData(功能键, key_u); }
            if ((kbh.vkCode == 85) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 86) && (wParam == 256)) { SendData(功能键, key_v); }
            if ((kbh.vkCode == 86) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 87) && (wParam == 256)) { SendData(功能键, key_w); key_W.ForeColor = Color.Red; }
            if ((kbh.vkCode == 87) && (wParam == 257)) { UpKeys(); key_W.ForeColor = Color.Black; }
            if ((kbh.vkCode == 88) && (wParam == 256)) { SendData(功能键, key_x); key_X.ForeColor = Color.Red; }
            if ((kbh.vkCode == 88) && (wParam == 257)) { UpKeys(); key_X.ForeColor = Color.Black; }
            if ((kbh.vkCode == 89) && (wParam == 256)) { SendData(功能键, key_y); key_Y.ForeColor = Color.Red; }
            if ((kbh.vkCode == 89) && (wParam == 257)) { UpKeys(); key_Y.ForeColor = Color.Black; }
            if ((kbh.vkCode == 90) && (wParam == 256)) { SendData(功能键, key_z); key_Z.ForeColor = Color.Red; }
            if ((kbh.vkCode == 90) && (wParam == 257)) { UpKeys(); key_Z.ForeColor = Color.Black; }
            /********************************************************************/
            /***********************单独按下主键盘1-0*************************/
            if ((kbh.vkCode == 49) && (wParam == 256)) { SendData(功能键, key__1); }
            if ((kbh.vkCode == 49) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 50) && (wParam == 256)) { SendData(功能键, key__2); }
            if ((kbh.vkCode == 50) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 51) && (wParam == 256)) { SendData(功能键, key__3); }
            if ((kbh.vkCode == 51) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 52) && (wParam == 256)) { SendData(功能键, key__4); }
            if ((kbh.vkCode == 52) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 53) && (wParam == 256)) { SendData(功能键, key__5); }
            if ((kbh.vkCode == 53) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 54) && (wParam == 256)) { SendData(功能键, key__6); }
            if ((kbh.vkCode == 54) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 55) && (wParam == 256)) { SendData(功能键, key__7); }
            if ((kbh.vkCode == 55) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 56) && (wParam == 256)) { SendData(功能键, key__8); }
            if ((kbh.vkCode == 56) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 57) && (wParam == 256)) { SendData(功能键, key__9); }
            if ((kbh.vkCode == 57) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 48) && (wParam == 256)) { SendData(功能键, key__0); }
            if ((kbh.vkCode == 48) && (wParam == 257)) { UpKeys(); }
            /***********************单独按下主键盘1-0*************************/
            /**************************单独按下标点****************************/
            if ((kbh.vkCode == 189) && (wParam == 256)) { SendData(功能键, 标点[0]); }
            if ((kbh.vkCode == 189) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 187) && (wParam == 256)) { SendData(功能键, 标点[1]); }
            if ((kbh.vkCode == 187) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 219) && (wParam == 256)) { SendData(功能键, 标点[2]); }
            if ((kbh.vkCode == 219) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 221) && (wParam == 256)) { SendData(功能键, 标点[3]); }
            if ((kbh.vkCode == 221) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 220) && (wParam == 256)) { SendData(功能键, 标点[4]); }
            if ((kbh.vkCode == 220) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 186) && (wParam == 256)) { SendData(功能键, 标点[5]); }
            if ((kbh.vkCode == 186) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 222) && (wParam == 256)) { SendData(功能键, 标点[6]); }
            if ((kbh.vkCode == 222) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 188) && (wParam == 256)) { SendData(功能键, 标点[7]); }
            if ((kbh.vkCode == 188) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 190) && (wParam == 256)) { SendData(功能键, 标点[8]); }
            if ((kbh.vkCode == 190) && (wParam == 257)) { UpKeys(); }
            if ((kbh.vkCode == 191) && (wParam == 256)) { SendData(功能键, 标点[9]); }
            if ((kbh.vkCode == 191) && (wParam == 257)) { UpKeys(); }
            /**************************单独按下标点****************************/
            /**************************单独按数字键盘****************************/
            if ((kbh.vkCode == 97) && (wParam == 256)) { SendData(功能键, key_Number1); key_Num1.ForeColor = Color.Red; }
            if ((kbh.vkCode == 97) && (wParam == 257)) { UpKeys(); key_Num1.ForeColor = Color.Black; }
            if ((kbh.vkCode == 98) && (wParam == 256)) { SendData(功能键, key_Number2); key_Num2.ForeColor = Color.Red; }
            if ((kbh.vkCode == 98) && (wParam == 257)) { UpKeys(); key_Num2.ForeColor = Color.Black; }
            if ((kbh.vkCode == 99) && (wParam == 256)) { SendData(功能键, key_Number3); key_Num3.ForeColor = Color.Red; }
            if ((kbh.vkCode == 99) && (wParam == 257)) { UpKeys(); key_Num3.ForeColor = Color.Black; }
            if ((kbh.vkCode == 100) && (wParam == 256)) { SendData(功能键, key_Number4); key_Num4.ForeColor = Color.Red; }
            if ((kbh.vkCode == 100) && (wParam == 257)) { UpKeys(); key_Num4.ForeColor = Color.Black; }
            if ((kbh.vkCode == 101) && (wParam == 256)) { SendData(功能键, key_Number5); key_Num5.ForeColor = Color.Red; }
            if ((kbh.vkCode == 101) && (wParam == 257)) { UpKeys(); key_Num5.ForeColor = Color.Black; }
            if ((kbh.vkCode == 102) && (wParam == 256)) { SendData(功能键, key_Number6); key_Num6.ForeColor = Color.Red; }
            if ((kbh.vkCode == 102) && (wParam == 257)) { UpKeys(); key_Num6.ForeColor = Color.Black; }
            if ((kbh.vkCode == 103) && (wParam == 256)) { SendData(功能键, key_Number7); key_Num7.ForeColor = Color.Red; }
            if ((kbh.vkCode == 103) && (wParam == 257)) { UpKeys(); key_Num7.ForeColor = Color.Black; }
            if ((kbh.vkCode == 104) && (wParam == 256)) { SendData(功能键, key_Number8); key_Num8.ForeColor = Color.Red; }
            if ((kbh.vkCode == 104) && (wParam == 257)) { UpKeys(); key_Num8.ForeColor = Color.Black; }
            if ((kbh.vkCode == 105) && (wParam == 256)) { SendData(功能键, key_Number9); key_Num9.ForeColor = Color.Red; }
            if ((kbh.vkCode == 105) && (wParam == 257)) { UpKeys(); key_Num9.ForeColor = Color.Black; }
            if ((kbh.vkCode == 96) && (wParam == 256)) { SendData(功能键, key_Number0); key_Num0.ForeColor = Color.Red; }  
            if ((kbh.vkCode == 96) && (wParam == 257)) { UpKeys(); key_Num0.ForeColor = Color.Black; }
            if ((kbh.vkCode == 144) && (wParam == 256)) { SendData(功能键, key_Num_Lock); Num_Lock.ForeColor = Color.Red; }     //key_Num_Lock
            if ((kbh.vkCode == 144) && (wParam == 257)) { UpKeys(); Num_Lock.ForeColor = Color.Black; }
            /**************************单独按数字键盘****************************/

            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
    }
}

 6、窗体设计

ch9329,电脑,c#,windows

 7、运行时窗体界面

ch9329,电脑,c#,windows

8、运行效果

可实现基本的键盘鼠标操作,暂时无法进行鼠标拖动,鼠标分辨率由于使用timer控件,所以如果分辨率调得太高会影响键盘输入。默认我只设置了高中低三挡,有需要的可以在程序中自行修改。

 文章来源地址https://www.toymoban.com/news/detail-728285.html

 

 

 

到了这里,关于串口线转CH9329,HID控制电脑方案(适用于无法上外网的电脑)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包