魔兽改键

</pre>突然间想起玩dota,网上去找改建精灵,但现在的大多下载站都会带着些东西,没有纯粹的。就想着自己写一个,搜索竟然有源代码<p></p><p><a target=_blank target="_blank" href="http://http://blog.csdn.net/sohighthesky/article/details/4550294">http://blog.csdn.net/sohighthesky/article/details/4550294</a></p><p>有些内容现在还理解不能,先照着弄一下改一下源码。</p><p>部分源码:</p><p></p><pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace war3Keywizard
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        war3Keywizard.KeyboardHook hook = new war3Keywizard.KeyboardHook();
        private void Form1_Load(object sender, EventArgs e)
        {
            
            hook.OnKeyDownEvent += new KeyEventHandler(hook_OnKeyDownEvent);
            KeyPreview = true;
        }
        /**
        void hook_OnKeyDownEvent(object sender, KeyEventArgs e)
        {
            //在这里就可以截获到所有的键盘按键了  
            textBox1.Text = e.KeyValue.ToString();
     
        }
    */
        [DllImport("USER32.DLL")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);



        private const int KEY_QUOTLEFT = 219;//键盘上 [ 键的代码  
        private const int KEY_QUOTRIGHT = 221;//键盘上 ] 键的代码  
        bool isHookEnable = false;//全局变量,指示Hook是否作用  
        private const int WM_KEYDOWN = 0x100;
        private const int WM_KEYUP = 0x101;
        private const int WM_CHAR = 0x102;
        static KeyValChangeMap kMP = new KeyValChangeMap();
        private void Intstart()
        {
            if (!string.IsNullOrEmpty(textBox2.Text))
            {
                //1
                kMP.insertKeypair((int)textBox2.Text[0], 97);
            }
            if (!string.IsNullOrEmpty(textBox3.Text))
            {
                //2
                kMP.insertKeypair( (int)textBox3.Text[0],98);
            }
            if (!string.IsNullOrEmpty(textBox4.Text))
            {
                //4
                kMP.insertKeypair((int)textBox4.Text[0],100);
            }
            if (!string.IsNullOrEmpty(textBox5.Text))
            {
                //5
                kMP.insertKeypair((int)textBox5.Text[0],101);
            }
            if (!string.IsNullOrEmpty(textBox6.Text))
            {
                //7
                kMP.insertKeypair( (int)textBox6.Text[0],103);
            }
            if (!string.IsNullOrEmpty(textBox7.Text))
            {
                //9
                kMP.insertKeypair( (int)textBox7.Text[0],104);
            }


        }

        void hook_OnKeyDownEvent(object sender, KeyEventArgs e)
        {
           textBox1.Text = e.KeyCode.ToString();
            if (e.KeyCode == Keys.Scroll)
            {
                isHookEnable = !isHookEnable;
                string info = isHookEnable ? "quickey-开启" : "quickey-停用";
                if (isHookEnable)
                {
                    Intstart();
                    notifyIcon1.Visible = true;
                    notifyIcon1.Text = info;
                }
                else
                {
                    kMP.clear();
                    notifyIcon1.Visible = false;
                }
            }
            if (isHookEnable)
            {
                IntPtr war3 = FindWindow(null, "Warcraft III");
                //   IntPtr war3 = FindWindow(null, "Warcraft III");  
                if (war3 != IntPtr.Zero)
                {
                    //SetForegroundWindow(war3);//将war3窗口置前,这一句我自己测试时可以不用,但去掉这一句后朋友那里说不行  
                    SendMessage(war3, WM_KEYDOWN, KEY_QUOTLEFT, 0);//按下[键就可以显示友军的血了  
                    SendMessage(war3, WM_KEYDOWN, KEY_QUOTRIGHT, 0);

                    int finalkey = kMP.getKeyChange((int)e.KeyCode);
                    if (finalkey != (int)e.KeyCode)
                    {
                        
                            //SendMessage(war3, WM_CHAR, finalkey, 0);//按了 
                        SendMessage(war3, WM_KEYDOWN, finalkey, 0);//按下  
                       SendMessage(war3, WM_KEYUP, finalkey, 0);//松开  
                    }
                }
            }
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBox8.Text +="\r\n"+ e.KeyChar.ToString();
        }
    }
    //改建的值

    class KeyValChangeMap
        {
        //小键盘区键码 0=96
            List<int> originKey = new List<int>();
            List<int> finalKey = new List<int>();
            public void insertKeypair(int keyO, int keyF)
            {
                originKey.Add(keyO);
                finalKey.Add(keyF);

            }
            public void clear()
            {
                originKey.Clear();
                finalKey.Clear();
            }
            public int getKeyChange(int keyO)
            {
                int loc= originKey.IndexOf(keyO);
                if (loc < 0)
                {
                    return keyO;
                }
                else
                {
                    return finalKey[loc];
                }
            }
        }


        
   
}

有了基本的功能,源码还有不少不懂得地方,需要继续研究和改进。


吐槽一下vs2015,真心受不了,好不容易下了个iso镜像,安装的时候貌似还跑到原站点去下了,好不容易装上了,安装个语言居然还要那么久,这一天都被耽误了。调试程序的时候可以改代码,却不让继续运行,呜呜,我在按键处打断点每次都得先删除打的字母才能调试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值