用C#钩子写一个改键外挂

news/2024/11/9 10:01:03/

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

改键是一种习惯,比如在玩儿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


http://www.ppmy.cn/news/570471.html

相关文章

笑傲江湖客户端服务器地址修改,《笑傲江湖》改键调整操作手把手教你玩笑傲...

《笑傲江湖》采用全新引擎AngelicaIII打造&#xff0c;秉承原著武侠精髓&#xff0c;首推新派动作武侠网游概念——融入动作及格斗游戏要素&#xff0c;强调真实的打击感与流畅的动作连贯度&#xff0c;并运用方向判定、位移闪避、移动战斗、攻防一体等多重技术手段&#xff0c…

dota2html颜色代码,技术向教程 如何在DotA2中使用彩色字体

技术向教程 如何在DotA2中使用彩色字体 编译:M82A1炸菜 如何使用16进制编辑器对DotA2文字就行颜色编辑 需要:十六进制编辑器、记事本、控制台 1.打开steam平台 2.在游戏库中选择DotA2,右击,选择属性 3.设置启动项,填入“-console”,确定 4.打开DotA2游戏,并且使用“`”键…

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

上一篇文章已经写了基于 普通键盘钩子&#xff08;单线程DLL&#xff09;来实现dota改键。http://blog.csdn.net/a576323437/article/details/8037138 这一次&#xff0c;基于 低级键盘钩子 &#xff08;全局 免DLL注入&#xff09;来实现。先看下总结&#xff1a; 低级键盘钩…

dota2游戏c语言,新手科普:Dota2操作按键设置和游戏设置详解

DOTA2的设置里有四个大项。控制&#xff0c;游戏&#xff0c;视频&#xff0c;音频。 后面两个自己根据自己配置水平设置就是。我主要讲控制和游戏。 控制&#xff1a; 页面1&#xff1a;单位行为 攻击&#xff0c;移动&#xff0c;停止&#xff0c;保持这种命令就不用说了。这…

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

呼。。终于可以摒弃网上带广告的改键工具了。。 历经三天&#xff0c;写出自己的dota改键软件最简单版了。 还学习了两个新知识&#xff0c;钩子和动态链接库。下面以一个新手的角度&#xff0c;总结下这三天遇到的大小问题。 一般钩子在什么时刻被调用&#xff1f; 操作系统把…

War3Tool dota改键v3.3版

wartool魔兽全屏改键功能&#xff1a;1.支持11平台自定义改建&#xff0c;自动进局域网(同类软件暂时没发现这个功能)2.技能改键&#xff0c;可以有效的切换适合你的技能键3.war3路径扫描&#xff0c;运行本程序一键就能打开war3 &#xff08;翻遍用户&#xff09; 喜欢的朋友可…

Dota改键

利用全局钩子 制作一个个性化的dota游戏改键&#xff01; dll部分&#xff1a; // FileName: add.cpp#include <Windows.h>/* 定义全局变量 */ HWND g_hwnd NULL; HHOOK g_hKeyboard NULL;// 设置数据段 #pragma data_seg("MySec") static WORD g_keyNum[6]{…

MySQL - 第10节 - MySQL索引特性

1.索引的概念 索引的概念&#xff1a; • 数据库表中存储的数据都是以记录为单位的&#xff0c;如果在查询数据时直接一条条遍历表中的数据记录&#xff0c;那么查询的时间复杂度将会是O(N)。 • 索引的价值在于提高海量数据的检索速度&#xff0c;只要执行了正确的创建索引的操…