前两天放假无聊,天气太冷不想出门,玩起了很多年前玩过的一款游戏:骑士。太久没玩,手指灵活度不够,玩的是一塌糊涂。于是想起了大名鼎鼎的按键精灵,下载,写脚本,再上游戏,解放了双手的感觉就是不一样,轻松+惬意。今天是新年上班第一天,比较有空,想起来这件事,秉承重复造轮子的指导方针,经过一下午的google,大概功能已经写好,能够满足骑士中战士这个职业最基本的需求.

  有点标题党的感觉~~这东西充其量只能说是按键辅助工具,和大名鼎鼎的按键精灵比起来实在是小巫见大巫了。

  前两天放假无聊,天气太冷不想出门,玩起了很多年前玩过的一款游戏:骑士。太久没玩,手指灵活度不够,玩的是一塌糊涂。于是想起了大名鼎鼎的按键精灵,下载,写脚本,再上游戏,解放了双手的感觉就是不一样,轻松+惬意。今天是新年上班第一天,比较有空,想起来这件事,秉承重复造轮子的指导方针,经过一下午的google,大概功能已经写好,能够满足骑士中战士这个职业最基本的需求。以下是代码:

    public class hook
    {
        // Fields
        private static int hHook;
        private HookProc KeyBoardHookProcedure;

        static Dictionary<string, Thread> dict = new Dictionary<string, Thread>();

        // Methods
        static hook() { }
        public hook() { }
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
        public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("USER32.DLL")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
        public void Hook_Clear()
        {
            bool flag = true;
            if (hHook != 0)
            {
                flag = UnhookWindowsHookEx(hHook);
                hHook = 0;
            }
            if (!flag)
            {
                throw new Exception("取消hook失败!");
            }
        }
        public void Hook_Start()
        {
            if (hHook == 0)
            {
                this.KeyBoardHookProcedure = new HookProc(hook.KeyBoardHookProc);
                hHook = SetWindowsHookEx(13, this.KeyBoardHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
                if (hHook == 0)
                {
                    MessageBox.Show("设置Hook失败!");
                    this.Hook_Clear();
                }
            }
        }
        [DllImport("User32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        public static void 战士抖杀()
        {
            while (true)
            {
                byte num1 = (byte)Keys.E;
                keybd_event(num1, 0, 0, 0);
                Thread.Sleep(100);
                keybd_event(num1, 0, 2, 0);
                Thread.Sleep(100);
                keybd_event(num1, 0, 0, 0);
                Thread.Sleep(100);
                keybd_event(num1, 0, 2, 0);
                Thread.Sleep(100);
                byte num2 = (byte)Keys.D4;
                keybd_event(num2, 0, 0, 0);
                Thread.Sleep(100);
                keybd_event(num2, 0, 2, 0);
                Thread.Sleep(100);
                keybd_event(num1, 0, 0, 0);
                Thread.Sleep(100);
                keybd_event(num1, 0, 2, 0);
                Thread.Sleep(100);
                keybd_event(num1, 0, 0, 0);
                Thread.Sleep(100);
                keybd_event(num1, 0, 2, 0);
                Thread.Sleep(100);
                byte num4 = (byte)Keys.R;
                keybd_event(num4, 0, 0, 0);
                Thread.Sleep(100);
                keybd_event(num4, 0, 2, 0);
                Thread.Sleep(100);
            }
        }

        public static int KeyBoardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            KeyBoardHookStruct input = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));

            if (input.vkCode == (int)Keys.F11)
            {
                IntPtr ptr = GetForegroundWindow();
                if (ptr != IntPtr.Zero)
                {
                    if (dict.Count < 1)
                    {
                        ThreadStart threadStart = new ThreadStart(战士抖杀);
                        Thread thread = new Thread(threadStart);
                        thread.Start();
                        dict.Clear();
                        dict.Add("战士抖杀", thread);
                    }
                }
                return 1;
            }
            else if (input.vkCode == (int)Keys.F12)
            {
                foreach (var th in dict)
                {
                    Thread thread = th.Value;
                    if (thread != null && thread.IsAlive)
                    {
                        if (!thread.Join(3))
                        {
                            thread.Abort();
                        }
                    }
                }
                dict.Clear();
            }
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        // Nested Types
        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

        [StructLayout(LayoutKind.Sequential)]
        public class KeyBoardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
            public KeyBoardHookStruct() { }
        }
    }

  

版权声明:本文为JasonChou原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/JasonChou/archive/2013/01/04/2844408.html