日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

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

發(fā)布時(shí)間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用C#钩子写一个改键外挂 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我的微信群——軟件開(kāi)發(fā)測(cè)試工程師交流群,歡迎掃碼:

改鍵是一種習(xí)慣,比如在玩兒lol或者dota的時(shí)候。理論上玩兒什么游戲都可以改鍵。

做一個(gè)窗體(點(diǎn)擊Install——應(yīng)用改鍵,點(diǎn)擊Uninstall——撤銷(xiāo)應(yīng)用):

窗體定義代碼如下:

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運(yùn)行,找個(gè)游戲試一下,改鍵成功啦(按p成功打開(kāi)背包)~

?

轉(zhuǎn)載于:https://www.cnblogs.com/LanTianYou/p/5053682.html

總結(jié)

以上是生活随笔為你收集整理的用C#钩子写一个改键外挂的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。