我的微信群——软件开发测试工程师交流群,欢迎扫码:

改键是一种习惯,比如在玩儿lol或者dota的时候。理论上玩儿什么游戏都可以改键。

做一个窗体(点击Install——应用改键,点击Uninstall——撤销应用):

窗体定义代码如下:

using System.Windows.Forms;namespace KeysExchange
{partial class Form1{/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// Clean up any resources being used./// </summary>/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){this.intall_button = new System.Windows.Forms.Button();this.uninstall_button = new System.Windows.Forms.Button();this.comboBox1 = new System.Windows.Forms.ComboBox();this.comboBox2 = new System.Windows.Forms.ComboBox();this.label1 = new System.Windows.Forms.Label();this.SuspendLayout();// // intall_button// this.intall_button.Location = new System.Drawing.Point(179, 162);this.intall_button.Name = "intall_button";this.intall_button.Size = new System.Drawing.Size(75, 23);this.intall_button.TabIndex = 4;this.intall_button.Text = "Install";this.intall_button.UseVisualStyleBackColor = true;this.intall_button.Click += new System.EventHandler(this.intall_button_Click);// // uninstall_button// this.uninstall_button.Location = new System.Drawing.Point(179, 207);this.uninstall_button.Name = "uninstall_button";this.uninstall_button.Size = new System.Drawing.Size(75, 23);this.uninstall_button.TabIndex = 5;this.uninstall_button.Text = "Uninstall";this.uninstall_button.UseVisualStyleBackColor = true;this.uninstall_button.Click += new System.EventHandler(this.uninstall_button_Click);// // comboBox1// this.comboBox1.FormattingEnabled = true;this.comboBox1.Location = new System.Drawing.Point(54, 41);this.comboBox1.Name = "comboBox1";this.comboBox1.Size = new System.Drawing.Size(57, 21);this.comboBox1.TabIndex = 6;this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;// // comboBox2// this.comboBox2.FormattingEnabled = true;this.comboBox2.Location = new System.Drawing.Point(175, 41);this.comboBox2.Name = "comboBox2";this.comboBox2.Size = new System.Drawing.Size(57, 21);this.comboBox2.TabIndex = 7;this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;// // label1// this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(126, 44);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(43, 13);this.label1.TabIndex = 8;this.label1.Text = "改为:";// // Form1// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(301, 273);this.Controls.Add(this.label1);this.Controls.Add(this.comboBox2);this.Controls.Add(this.comboBox1);this.Controls.Add(this.uninstall_button);this.Controls.Add(this.intall_button);this.Name = "Form1";this.Text = "KeysExchange";this.ResumeLayout(false);this.PerformLayout();}#endregionprivate System.Windows.Forms.Button intall_button;private System.Windows.Forms.Button uninstall_button;private System.Windows.Forms.ComboBox comboBox1;private System.Windows.Forms.ComboBox comboBox2;private System.Windows.Forms.Label label1;}struct ComboItem{private string text;private string value;public ComboItem(string text, string value){this.text = text;this.value = value;}public override string ToString(){return this.text;}public string ToValue(){return this.value;}}
}

钩子代码如下:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;namespace KeysExchange
{public class KeyboardHookLib{private const int WH_KEYBOARD_LL = 13;      private delegate int HookHandle(int nCode, int wParam, IntPtr lParam);public delegate void ProcessKeyHandle(HookStruct param, out bool handle);private static int _hHookValue = 0;private HookHandle _KeyBoardHookProcedure;[StructLayout(LayoutKind.Sequential)]public class HookStruct{public int vkCode;public int scanCode;public int flags;public int time;public int dwExtraInfo;}[DllImport("user32.dll")]private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId);[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]private static extern bool UnhookWindowsHookEx(int idHook);[DllImport("user32.dll")]private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);[DllImport("kernel32.dll")]private static extern int GetCurrentThreadId();[DllImport("kernel32.dll")]private static extern IntPtr GetModuleHandle(string name);private IntPtr _hookWindowPtr = IntPtr.Zero;public KeyboardHookLib() { }private static ProcessKeyHandle _clientMethod = null;[DllImport("user32")]public static extern int GetKeyboardState(byte[] pbKeyState);[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]private static extern short GetKeyState(int vKey);private const int WM_KEYDOWN = 0x100;//KEYDOWNprivate const int WM_KEYUP = 0x101;//KEYUPprivate const int WM_SYSKEYDOWN = 0x104;//SYSKEYDOWNprivate const int WM_SYSKEYUP = 0x105;//SYSKEYUPpublic void InstallHook(ProcessKeyHandle clientMethod){_clientMethod = clientMethod;if (_hHookValue == 0){_KeyBoardHookProcedure = new HookHandle(OnHookProc);_hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);_hHookValue = SetWindowsHookEx(WH_KEYBOARD_LL, _KeyBoardHookProcedure, _hookWindowPtr, 0);if (_hHookValue == 0) UninstallHook();}}public void UninstallHook(){if (_hHookValue != 0){if (UnhookWindowsHookEx(_hHookValue)){_hHookValue = 0;}}}private static int OnHookProc(int nCode, int wParam, IntPtr lParam){if (nCode >= 0){HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct));if (_clientMethod != null){bool handle = false;///Tylan: Judge if the event is KeyDown or not.if (lParam.ToInt32() > 0 && wParam == 0x100){_clientMethod(hookStruct, out handle);}if (handle) return 1; }}return CallNextHookEx(_hHookValue, nCode, wParam, lParam);}}
}

逻辑部分代码如下:

using System;
using System.Windows.Forms;namespace KeysExchange
{public partial class Form1 : Form{private KeyboardHookLib _keyboardHook = null;public Form1(){InitializeComponent();for (int alp = 65; alp <= 90; alp++){ComboItem item = new ComboItem(((Keys)alp).ToString(), alp.ToString());comboBox1.Items.Add(item);comboBox2.Items.Add(item);}}private void intall_button_Click(object sender, EventArgs e){//Install the hook._keyboardHook = new KeyboardHookLib();_keyboardHook.InstallHook(this.OnKeyPress);}private void uninstall_button_Click(object sender, EventArgs e){//Cancel the hook.if (_keyboardHook != null) _keyboardHook.UninstallHook();}public void OnKeyPress(KeyboardHookLib.HookStruct hookStruct, out bool handle){handle = false;if (((Keys)hookStruct.vkCode).ToString() == comboBox1.SelectedItem.ToString()) {handle = true;//Exchange the keys.hookStruct.vkCode = int.Parse(((ComboItem)comboBox2.SelectedItem).ToValue());Keys key = (Keys)hookStruct.vkCode;//MessageBox.Show((key == Keys.None ? "" : key.ToString()));
                System.Windows.Forms.SendKeys.Send(key.ToString().ToLower());}}}
}

F5运行,找个游戏试一下,改键成功啦(按p成功打开背包)~

转载于:https://www.cnblogs.com/LanTianYou/p/5053682.html

用C#钩子写一个改键外挂相关推荐

  1. AutoHotKey写一个改键的小脚本

    应D君邀请写一个小的改键软件,用了一上午选择使用的脚本语言,下午开始去写.晚饭的时候,基本的功能完成了.先所说我要完成的脚本的功能吧.软件有一定编程基础的D君用,用户比较特定,功能越简单越好.因此,软 ...

  2. 基于键盘钩子的dota改键(单线程+DLL)MFC实现(源码+总结)

    呼..终于可以摒弃网上带广告的改键工具了.. 历经三天,写出自己的dota改键软件最简单版了. 还学习了两个新知识,钩子和动态链接库.下面以一个新手的角度,总结下这三天遇到的大小问题. 一般钩子在什么 ...

  3. 基于低级键盘钩子的dota改键(全局+免DLL注入)MFC实现(源码+总结)

    上一篇文章已经写了基于 普通键盘钩子(单线程+DLL)来实现dota改键.http://blog.csdn.net/a576323437/article/details/8037138 这一次,基于 ...

  4. 写魔兽改键时遇到的问题

    由于在DLL中定义了变量,所以要在读取ini文件时赋值,但遇到诡异的一个问题,每次赋值之后按一下键盘,进入HOOK,发现变量值又变为初始值,经仔细思考再查阅网络才知道,每次进入钩子,会出现dll冲入现 ...

  5. 关于魔兽改键。。。类似11的

    事情是这样的--闲的无聊玩了一盘卡尔的DOTA,结果发现卡尔的按键真的是十分XX啊--于是就在各个网站找那种类似于11的魔兽改键工具,不是改快捷键,而是直接按照技能的位置来改键的.可是没有找到,而11 ...

  6. 魔兽重置版改键+喊话

    魔兽重置版改键+喊话 原创软件,绝无广告 附代码 **长总求:自己写的改键+喊话软件 ** 最近在玩魔兽争霸3重置版 别的不评价了,没有改键功能非常麻烦,自己做了一个. 好坏就不评论了,就当给当年补票 ...

  7. 给网游写一个挂吧(三) – 启动外挂下

    前面的文章给网游写一个挂吧 – 启动外挂上介绍了输入法注入的方法,本文解释第二种方法. 有的游戏限制比较多,可能会将输入法注入也禁用掉--这个时候就需要另想方法了.其实我们的目的很简单,就是要让不知道 ...

  8. C#:键盘钩子的使用,实现键盘屏蔽 及 全局改键功能

    1.引入 由于需要使用动态链接库及Process进程类,所以需要在项目中引入命名空间 using System.Diagnostics; using System.Runtime.InteropSer ...

  9. 使用java来写一个游戏外挂-内存修改程序(辅助-开篇)

    很久以前研究过如何使用java写一个内存读写的程序,但是java都是知道的 它是在虚拟机上进行运行的,所以没办法进行内存的读写 所以用到了jan来执行windows自带的 kernel32.dll程序 ...

最新文章

  1. 【Python之路】第二篇--初识Python
  2. 多层感知机MLP、RBF网络、Hopfield网络、自组织映射神经网络、神经网络算法地图
  3. php mysql cpu100_MySQL服务器进程CPU占用100%的解决方法
  4. 【剑指offer-Java版】06重建二叉树
  5. 自由落体和抛物线运动
  6. Multimap的遍历和删除(很重要)
  7. Android4.0 以后不允许在主线程进行网络连接
  8. 在Ubuntu 18.04上实际安装OpenJDK 11
  9. 熊猫分发_熊猫新手:第一部分
  10. ThinkPHP(3.2)搭建简单留言板项目
  11. latex IEEE 模板 使用bib BibTeX
  12. Drool规则引擎介绍
  13. WordPress插件/WP资源下载管理插件 1.3.4
  14. Py 实现导线测量的内业计算
  15. 将长表格图片转Excel表格
  16. 计算机安装xp蓝屏怎么办,联想笔记本装xp系统蓝屏解决方法
  17. Sphinx 全文搜索引擎 介绍指南
  18. 程序员必备的提升工作效率的方法
  19. TCAM与HASH表的差异
  20. 世事无常?还是......

热门文章

  1. 2021-2027年中国玩具行业市场研究及前瞻分析报告
  2. docker常用命令详解
  3. Python 笔试面试及常用技巧 (1)
  4. leetcode 5. Longest Palindromic Substring 字符串中的最长回文数 逐步从O(n^2)优化至线性时间
  5. 192. Word Frequency 使用shell统计词频
  6. pycharm导包错误
  7. pytorch中查看gpu信息
  8. MegEngine亚线性显存优化
  9. 深度学习目标检测详细解析以及Mask R-CNN示例
  10. 2021年大数据ELK(九):使用VSCode测试分词器