c# 改键-之魔兽改键(Hook)

      大学里有几个没玩过的dota,玩过的人都知道这游戏要用到一个辅助软件,即改键的软件,由于dota这款游戏的快捷键众多且杂乱无章,所以就需要一款改键的软件把那些快捷键改成我们顺手的,比如我们可以把P改成Q,把B改成W,把C改成E,这样一来QWE是一排紧挨着的按键那么我们操作dota这款游戏起来是不是很顺手了呢,好,说完改键软件的作用下面正题

      不用我说大家也都知道改键用到的就是Hook,关于hook的代码网上不是一般的多,在这里就不说了,说了我也是借鉴别人的等于copy了就不好看了,除了钩子hook那么剩下的就很简单了,我不过是七凑八凑组成个改键的软件而已,那我为什么还要写它呢,是因为网上关于改键的程序例子少之又少,我在这里给那些想搜索这方面内容的人提供一些思路,懒得一句一句的解释了,直接贴代码付工程比什么都强

      对了,先来张程序的图片吧

c# 改键-之魔兽改键(Hook)

c# 改键-之魔兽改键(Hook)

c# 改键-之魔兽改键(Hook)

     前两张图片是N年前写的,代码也找不到了,最近想总结一下又写了一个,写了个大概,小功能尚未完善,正式第三个,写此程序需要三个数组,一个数组保存textbox,第二个数组保存输入的按键的键码值(用于界面填写规则,判断按键是否有重复或者有冲突),第三个数组也是保存按键的键码值,但是与第二个数组不同,它保存的键码值用于钩子改键的并不是用于检查填写的改键是否有冲突的。我为了简单方便并没有采用3个数组,只设了一个二维数组,另两个数组不过把其值添加进textbox的属性里了,前面说3个不过是为了好理解,OK贴代码

c# 改键-之魔兽改键(Hook)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using Microsoft.Win32;
using myLib;

//email:zzyhost@gmail.com


namespace WindowsFormsApplication57
{


    public partial class Form1 : Form
    {

        private TB[,] t = new TB[14, 2];

        public Form1()
        {
            InitializeComponent(); myLib.UI.YuanJiao(this); myLib.UI.Move(this);

            hook = new Hook();
            hook.SetKeyboardHook(KbHookProc);


            for (int i = 0; i < t.GetLength(0); i++)
            {
                for (int j = 0; j < t.GetLength(1); j++)
                {
                    t[i, j] = new TB();
                    t[i, j].Multiline = true;
                    t[i, j].ImeMode = ImeMode.Disable;
                    t[i, j].TextAlign = HorizontalAlignment.Center;
                    t[i, j].Size = new Size(30, 20);
                    t[i, j].Index0 = i;
                    t[i, j].Index1 = j;

                    if (i < 4) t[i, j].Location = new Point(j * 40, i * 25);
                    else if (i < 8) t[i, j].Location = new Point(j * 40 + 90, (i - 4) * 25);
                    else if (i < 11) t[i, j].Location = new Point(j * 40 + 150, (i - 8) * 38);
                    else t[i, j].Location = new Point(j * 40 + 200, (i - 11) * 38);

                    if (i == 8 && j == 0) { t[i, j].Visible = false; t[i, j].TempValue = t[i, j].KeyValue = 103; }
                    else if (i == 9 && j == 0) { t[i, j].Visible = false; t[i, j].TempValue = t[i, j].KeyValue = 100; }
                    else if (i == 10 && j == 0) { t[i, j].Visible = false; t[i, j].TempValue = t[i, j].KeyValue = 97; }
                    else if (i == 11 && j == 0) { t[i, j].Visible = false; t[i, j].TempValue = t[i, j].KeyValue = 104; }
                    else if (i == 12 && j == 0) { t[i, j].Visible = false; t[i, j].TempValue = t[i, j].KeyValue = 101; }
                    else if (i == 13 && j == 0) { t[i, j].Visible = false; t[i, j].TempValue = t[i, j].KeyValue = 98; }

                    panel1.Controls.Add(t[i, j]);

                    t[i, j].KeyDown += new KeyEventHandler(t_KeyDown);
                    t[i, j].KeyPress += new KeyPressEventHandler(t_KeyPress);
                }
            }

            using (RegistryKey k1 = Registry.CurrentUser, k2 = k1.CreateSubKey(regP1))
            {
                string[] keyNames = k2.GetSubKeyNames();
                if (keyNames.Length != 0)
                {
                    this.comboBox1.Items.AddRange(keyNames);
                    this.comboBox1.SelectedIndex = (int)k2.GetValue("LastSelectedIndex");
                }
            }

        }






        private GraphicsPath GetGraphicsPath(Rectangle rc, int r)
        {
            int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height;
            GraphicsPath gpath = new GraphicsPath();
            //gpath.AddEllipse(rc);
            gpath.AddArc(x, y, r, r, 180, 90);//左上角圆弧
            gpath.AddArc(x + w - r, y, r, r, 270, 90);//右上角圆弧
            gpath.AddArc(x + w - r, y + h - r, r, r, 0, 90);//右下角圆弧
            gpath.AddArc(x, y + h - r, r, r, 90, 90);//左下角圆弧
            gpath.CloseFigure();//闭合
            return gpath;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnLoad(e);

            Color c = Color.SteelBlue;

            Graphics g = e.Graphics;

            g.SmoothingMode = SmoothingMode.AntiAlias;//平滑



            Rectangle rect = new Rectangle(0, 0, this.Width, 30);


            LinearGradientBrush fillBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, 35), c, Color.White);

            g.FillRectangle(fillBrush, rect);
            fillBrush.Dispose();


            Rectangle rect1 = new Rectangle(0, 0, this.Width-1, this.Height-1);

            //rect1.Inflate(-1, -1);//扩充
            GraphicsPath path = this.GetGraphicsPath(rect1, 10);


            using (Pen pen = new Pen(c))
            {
                g.DrawPath(pen, path);
                g.DrawLine(pen, 0, 30, this.Width, 30);
            }

            Rectangle rect2 = new Rectangle(6, 111, 280, 107);
            GraphicsPath path2 = this.GetGraphicsPath(rect2, 10);
            using (Pen pen = new Pen(c))
            {
                g.DrawPath(pen, path2);
                g.DrawLine(pen, 91, 118, 91, 212);
                g.DrawLine(pen, 180, 118, 180, 212);
            }

            Font drawFont = new Font("Arial", 8);
            using (SolidBrush drawBrush = new SolidBrush(Color.Black))
            {
                g.DrawString("7-", drawFont, drawBrush, 188, 120);
                g.DrawString("4-", drawFont, drawBrush, 188, 159);
                g.DrawString("1-", drawFont, drawBrush, 188, 196);
                g.DrawString("8-", drawFont, drawBrush, 239, 120);
                g.DrawString("5-", drawFont, drawBrush, 239, 159);
                g.DrawString("2-", drawFont, drawBrush, 239, 196);
            }
        }


        private void t_KeyDown(object sender, KeyEventArgs e)
        {
            TB mol = (TB)sender;
            int a = e.KeyValue;

            if ((a == 97 || a == 98 || a == 100 || a == 101 || a == 103 || a == 104) && mol.Index1 == 1) return;

            if (e.KeyCode == Keys.Back)
            {
                mol.Text = "";
                mol.TempValue = 0;
                return;
            }

            mol.TempValue = a;
            mol.Text = show(a);

            #region 改键填写规则
            if (mol.Index1 == 0)
            {
                for (int i = 0; i < t.GetLength(0); i++)
                {
                    if (t[i, 0].TempValue == a && i != mol.Index0) { t[i, 0].TempValue = 0; t[i, 0].Text = ""; }
                }
                if (t[mol.Index0, 1].TempValue == a) { t[mol.Index0, 1].TempValue = 0; t[mol.Index0, 1].Text = ""; }
            }
            else if (mol.Index1 == 1)
            {
                for (int i = 0; i < t.GetLength(0); i++)
                {
                    if (t[i, 1].TempValue == a && i != mol.Index0) { t[i, 1].TempValue = 0; t[i, 1].Text = ""; }
                }
                if (t[mol.Index0, 0].TempValue == a) { t[mol.Index0, 0].TempValue = 0; t[mol.Index0, 0].Text = ""; }
            }
            #endregion
        }

        private void t_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;//拦截按键,使textbox不显示按下的键
        }


        #region string show(int keyValue)//键码值转换成按键并显示在textbox中
        string show(int keyValue)
        {
            string keyName;
            switch (keyValue)
            {
                case 0: keyName = ""; break;
                case 16: keyName = "Shift"; break;
                case 17: keyName = "Ctrl"; break;
                case 18: keyName = "Alt"; break;
                case 27: keyName = "Esc"; break;
                case 13: keyName = "Enter"; break;
                case 91: keyName = "Win"; break;
                case 92: keyName = "Win"; break;

                case 160: keyName = "Shift"; break;//左Shift
                case 161: keyName = "Shift"; break;//右Shift
                case 162: keyName = "Ctrl"; break;
                case 163: keyName = "Ctrl"; break;
                case 164: keyName = "Alt"; break;
                case 165: keyName = "Alt"; break;

                case 186: keyName = ""; break;
                case 187: keyName = "="; break;
                case 188: keyName = ""; break;
                case 189: keyName = "-"; break;
                case 190: keyName = ""; break;
                case 191: keyName = "/"; break;
                case 192: keyName = "·"; break;
                case 219: keyName = "["; break;
                case 221: keyName = "]"; break;
                case 220: keyName = "‘\‘"; break;
                case 222: keyName = ""; break;

                default:
                    //if (keyValue >= 96 && keyValue <= 105)
                    //{
                    //    keyName = ((char)keyValue).ToString();
                    //}
                    keyName = ((Keys)keyValue).ToString(); break;
            }
            return keyName;

        }
        #endregion


        private myLib.Hook hook;
        private int KbHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            int vkCode = System.Runtime.InteropServices.Marshal.ReadInt32(lParam);

            if (nCode >= 0 && wParam == (IntPtr)0x100)
            {
                int k = 0;
                for (int i = 0; i < t.GetLength(0); i++)
                {
                    if (vkCode == t[i, 1].KeyValue)
                    {
                        for (int j = 0; j < t.GetLength(0); j++)
                        {
                            if (t[i, 0].KeyValue == t[j, 1].KeyValue && i != j)
                            {
                                t[j, 1].KeyValue = 0;
                                hook.press(t[i, 0].KeyValue);
                                t[j, 1].KeyValue = t[i, 0].KeyValue;
                                k = 1; break;
                            }
                        }
                        if (k == 0) { hook.press(t[i, 0].KeyValue); }
                        return 1;
                    }
                }
            }

            return Hook.CallNextHookEx(hook.KhookID, nCode, wParam, lParam);

        }

        private string regP1 = "Software\\魔兽改键\\自定义改键";
        private void Save_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "") { SaveAs_Click(null, null); return; }
            using (RegistryKey k1 = Registry.CurrentUser, k2 = k1.CreateSubKey(regP1 + "\\" + comboBox1.Text))
            {
                int a = 1;
                for (int i = 0; i < t.GetLength(0); i++)
                {
                    for (int j = 0; j < t.GetLength(1); j++)
                    {
                        k2.SetValue(a.ToString(), t[i, j].TempValue);
                        t[i, j].KeyValue = t[i, j].TempValue;
                        a++;
                    }
                }
            }
        }
        private int m = 0;//comboBox1_SelectedIndexChanged
        private void SaveAs_Click(object sender, EventArgs e)
        {
            m = 1;
            string name = "";
            Form2 f2 = new Form2();
            f2.ShowDialog();
            if (f2.DialogResult == DialogResult.OK)
            {
                name = f2.tt.Trim();
                f2.Close();
                if (comboBox1.Items.IndexOf(name) < 0)//若已存在就不用加了
                    this.comboBox1.Items.Add(name);
                this.comboBox1.Text = name;
                Save_Click(null, null);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)//
        {
            if (m == 1) { m = 0; return; }
            using (RegistryKey k1 = Registry.CurrentUser, k2 = k1.CreateSubKey(regP1 + "\\" + comboBox1.Text))
            {
                int a = 1;
                for (int i = 0; i < t.GetLength(0); i++)
                {
                    for (int j = 0; j < t.GetLength(1); j++)
                    {
                        t[i, j].TempValue = (int)k2.GetValue(a.ToString());
                        t[i, j].KeyValue = t[i, j].TempValue;
                        t[i, j].Text = show(t[i, j].KeyValue);
                        a++;
                    }
                }
            }
        }


        private void Del_Click(object sender, EventArgs e)
        {
            if (comboBox1.Items.Count == 0 || comboBox1.Text == "") { return; }
            using (RegistryKey k1 = Registry.CurrentUser, k2 = k1.CreateSubKey(regP1))
            {
                k2.DeleteSubKey(comboBox1.Text);
                comboBox1.Items.Remove(comboBox1.Text);
                if (comboBox1.Items.Count == 0) Empty_Click(null, null);
                else comboBox1.SelectedIndex = 0;//first
            }
        }

        private void Empty_Click(object sender, EventArgs e)
        {//comboBox1_SelectedIndexChanged don`t change
            for (int i = 0; i < t.GetLength(0); i++)
            {
                for (int j = 0; j < t.GetLength(1); j++)
                {
                    t[i, j].TempValue = 0;
                    t[i, j].KeyValue = 0;
                    t[i, j].Text = "";
                    this.comboBox1.Text = "";
                }
            }
        }


        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            using (RegistryKey k1 = Registry.CurrentUser, k2 = k1.CreateSubKey(regP1))
            {

                k2.SetValue("LastSelectedIndex", this.comboBox1.SelectedIndex);

            }
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }



    }
}
c# 改键-之魔兽改键(Hook)

 http://files.cnblogs.com/zzyhost/WindowsFormsApplication57.rar

c# 改键-之魔兽改键(Hook)

上一篇:solr多core的处理


下一篇:如何在Ruby中编写微服务?