魔兽改键程序修改

前两天的改键程序修改后基本可用,贴代码

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace war3Keywizard
{
    static class SysMessageConst
    {
        public const int WM_KEYDOWN = 0x100;//按下消息
        public const int WM_KEYUP = 0x101;//松开消息
        public const int WM_SYSKEYDOWN = 0x104;
        public const int WM_SYSKEYUP = 0x105;
    }
    public class KeyboardHook
    {

        //构造函数中安装钩子
        public KeyboardHook()
        {
            Start();
        }
        //析构函数中卸载钩子
        ~KeyboardHook()
        {
            Stop();
        }

        static int hKeyboardHook = 0;
        //鼠标常量
        public const int WH_KEYBOARD_LL = 13;

        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
        //声明键盘钩子事件类型
        HookProc KeyboardHookProcedure;

        //处理消息的函数 返回值  true:屏蔽消息 false:继续消息传递
        public delegate bool KeyValueWizardD(Int32 wParam);
        //按下键盘的处理
        public KeyValueWizardD keyDownvalWzard;
        //释放键盘的处理
        public KeyValueWizardD keyUpvalWizard;


        //安装钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        //下一个钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
        //卸载钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);
        [DllImport("kernel32")]
        public static extern int GetModuleHandle(string lpModuleName);



        public void Start()
        {
            if (hKeyboardHook == 0)
            {
                KeyboardHookProcedure = new HookProc(KeyboardHookProc);
                //hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]), 0);
                using (System.Diagnostics.Process curProcess = System.Diagnostics.Process.GetCurrentProcess())
                using (System.Diagnostics.ProcessModule curModule = curProcess.MainModule)
                    hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, (IntPtr)GetModuleHandle(curModule.ModuleName), 0);

                if (hKeyboardHook == 0)
                {
                    Stop();
                    throw new Exception("Set GlobalKeyboardHook failed!");
                }
            }
        }
        public void Stop()
        {
            bool retKeyboard = true;
            if (hKeyboardHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
                hKeyboardHook = 0;
            }
            if (!retKeyboard)
                throw new Exception("Unload GlobalKeyboardHook failed!");
        }
        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            KeyboardHookStruct MyKBHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
            int keyData = (int)MyKBHookStruct.vkCode;//keyValue
            //引发OnKeyDownEvent
            if (keyDownvalWzard != null && (wParam == SysMessageConst.WM_KEYDOWN || wParam == SysMessageConst.WM_SYSKEYDOWN))
            {
                if (keyDownvalWzard(keyData))
                {
                    return 1;//消息不继续传递
                }

            }
            if (keyUpvalWizard != null && (wParam == SysMessageConst.WM_KEYUP || wParam == SysMessageConst.WM_SYSKEYDOWN))
            {
                if (keyUpvalWizard(keyData)) return 1;
            }

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


        /// <summary>
        /// 声明键盘钩子的封送结构类型
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public class KeyboardHookStruct
        {
            public int vkCode;//表示一个1到254间的虚拟键盘码
            public int scanCode;//表示硬件扫描码
            public int flags;
            public int time;
            public int dwExtraInfo;
        }
    }
    public class war3keyWizard
    {
        public war3keyWizard()
        {
            KeyboardHook KBH = new KeyboardHook();
            KBH.keyDownvalWzard = keyDownproc;
            KBH.keyUpvalWizard = keyUpproc;
        }
        [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);

        //启动修改功能判断
        bool IsWizardON = false;
        public string info = "";
        IntPtr war3;//war3窗口句柄
        void KeyChange(Int32 wParam)
        {
            if (wParam == (int)Keys.Scroll)
            {
                IsWizardON = !IsWizardON;
                info = IsWizardON ? "Wizard-开启" : "Wizard-停用";
            }
            war3 = FindWindow(null, "Warcraft III");
            Logoutput.logput(info);
        }
        //处理键盘按下
        bool keyDownproc(Int32 wParam)
        {
            Logoutput.logput(wParam.ToString()+"被按下");

            KeyChange(wParam);
            if (IsWizardON && war3 != IntPtr.Zero)
            {
                int finalkey = KeyValChangeMap.getKeyChange(wParam);
                if (finalkey > 0)
                {
                    SendMessage(war3, SysMessageConst.WM_KEYDOWN, finalkey, 0);//按下  
                    return true;
                }

            }
            return false;
        }
        //处理键盘松开
        bool keyUpproc(Int32 wParam)
        {
            Logoutput.logput(wParam.ToString() + "被松开");
           // KeyChange(wParam);
            if (IsWizardON && war3 != IntPtr.Zero)
            {
                int finalkey = KeyValChangeMap.getKeyChange(wParam);
                if (finalkey > 0)
                {

                    SendMessage(war3, SysMessageConst.WM_KEYUP, finalkey, 0);//松开
                    return true;
                }

            }
            return false;
        }
        //设置键盘修改 KeyOL被修改键列表  KeyFL替换件
        public void SetKeyMap(List<int> KeyOL, List<int> KeyFL)
        {
            KeyValChangeMap.Clear();
            for (int i = 0; i < KeyOL.Count; i++)
            {
                KeyValChangeMap.insertKeypair(KeyOL[i], KeyFL[i]);
            }
        }
    }
    class KeyValChangeMap
    {
        static Dictionary<int, int> keyMap = new Dictionary<int, int>();
        //keyV 原建制  新键值
        static public void insertKeypair(int keyV, int keyK)
        {
            //插入对
            if (keyMap.ContainsKey(keyK) || keyMap.ContainsValue(keyV))
            {
                return;
            }
            keyMap.Add(keyK, keyV);

        }
        static public void Clear()
        {
            keyMap.Clear();
        }
        public static int getKeyChange(int keyK)
        {
            if (keyMap.ContainsKey(keyK))
            {
                return keyMap[keyK];
            }
            return -1;
        }
    }
    class Logoutput
    {
        static string war3wizartLog = "war3wizartLog.log";
       
       public static void logput(string log)
        {
            string fullfilepaht = System.IO.Path.Combine(Application.StartupPath, war3wizartLog);
            if (!System.IO.File.Exists(fullfilepaht))
                System.IO.File.Create(fullfilepaht);
            System.IO.FileStream fs = new System.IO.FileStream(fullfilepaht,System.IO.FileMode.Append);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
            sw.WriteLine(log);
            sw.Close();
            fs.Close();
           
        }
    }
}
界面

代码

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 W3key = new war3keyWizard();
        private void Form1_Load(object sender, EventArgs e)
        {
            KeyPreview = true;
        }




          static KeyValChangeMap kMP = new KeyValChangeMap();
        private void Intstart()
        {
            List<int> MiniBoard = new List<int>();
            List<int> ReplaceKey = new List<int>();
            if (!string.IsNullOrEmpty(textBox2.Text))
            {
                ReplaceKey.Add((int)textBox2.Text[0]);
                MiniBoard.Add(97);
            }
            if (!string.IsNullOrEmpty(textBox3.Text))
            {
                ReplaceKey.Add((int)textBox3.Text[0]);
                MiniBoard.Add(98);
            }
            if (!string.IsNullOrEmpty(textBox4.Text))
            {
                ReplaceKey.Add((int)textBox4.Text[0]);
                MiniBoard.Add(100);
            }
            if (!string.IsNullOrEmpty(textBox5.Text)) 
            {
                ReplaceKey.Add((int)textBox5.Text[0]);
                MiniBoard.Add(101);
            }
            if (!string.IsNullOrEmpty(textBox6.Text))
            {
                ReplaceKey.Add((int)textBox6.Text[0]);
                MiniBoard.Add(103);
          
            }
            if (!string.IsNullOrEmpty(textBox7.Text))
            {
                ReplaceKey.Add((int)textBox7.Text[0]);
                MiniBoard.Add(104);
            }
            W3key.SetKeyMap(MiniBoard, ReplaceKey);

        }
     

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Intstart();

        }
    }


     
        
   
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值