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

改键是一种习惯,比如在玩儿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(, );
this.intall_button.Name = "intall_button";
this.intall_button.Size = new System.Drawing.Size(, );
this.intall_button.TabIndex = ;
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(, );
this.uninstall_button.Name = "uninstall_button";
this.uninstall_button.Size = new System.Drawing.Size(, );
this.uninstall_button.TabIndex = ;
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(, );
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(, );
this.comboBox1.TabIndex = ;
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
//
// comboBox2
//
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Location = new System.Drawing.Point(, );
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(, );
this.comboBox2.TabIndex = ;
this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
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(, );
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();
} #endregion
private 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 = ;
private delegate int HookHandle(int nCode, int wParam, IntPtr lParam);
public delegate void ProcessKeyHandle(HookStruct param, out bool handle);
private static int _hHookValue = ;
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;//KEYDOWN
private const int WM_KEYUP = 0x101;//KEYUP
private const int WM_SYSKEYDOWN = 0x104;//SYSKEYDOWN
private const int WM_SYSKEYUP = 0x105;//SYSKEYUP public void InstallHook(ProcessKeyHandle clientMethod)
{
_clientMethod = clientMethod;
if (_hHookValue == )
{
_KeyBoardHookProcedure = new HookHandle(OnHookProc);
_hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
_hHookValue = SetWindowsHookEx(WH_KEYBOARD_LL, _KeyBoardHookProcedure, _hookWindowPtr, );
if (_hHookValue == ) UninstallHook();
}
} public void UninstallHook()
{
if (_hHookValue != )
{
if (UnhookWindowsHookEx(_hHookValue))
{
_hHookValue = ;
}
}
} private static int OnHookProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode >= )
{
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() > && wParam == 0x100)
{
_clientMethod(hookStruct, out handle);
}
if (handle) return ;
}
}
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 = ; alp <= ; 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成功打开背包)~

用C#钩子写一个改键外挂的更多相关文章

  1. 表单配置项写法,表单写成JSON数组套对象,一行是一个数组单位,一列是一个对象单位,然后再写一个公共组件读取这个配置,循环加载slot,外层载入slot的自定义部分,比如input select等,这种写法就是把组件嵌套改为配置方式

    表单配置项写法,表单写成JSON数组套对象,一行是一个数组单位,一列是一个对象单位,然后再写一个公共组件读取这个配置,循环加载slot,外层载入slot的自定义部分,比如input select等,这 ...

  2. 改变滚动条的原始样式: chrome 可以改变, IE只能变相关颜色,firfox好像也不好改。最好是自己写一个或是用插件

    相关作者链接地址: https://www.lyblog.net/detail/314.html 问题: 1.我在项目中遇到的问题: 在设置了::-webkit-scrollbar 后,滚动条不见了! ...

  3. 分享:计算机图形学期末作业!!利用WebGL的第三方库three.js写一个简单的网页版“我的世界小游戏”

    这几天一直在忙着期末考试,所以一直没有更新我的博客,今天刚把我的期末作业完成了,心情澎湃,所以晚上不管怎么样,我也要写一篇博客纪念一下我上课都没有听,还是通过强大的度娘完成了我的作业的经历.(当然作业 ...

  4. 一起写一个JSON解析器

    [本篇博文会介绍JSON解析的原理与实现,并一步一步写出来一个简单但实用的JSON解析器,项目地址:SimpleJSON.希望通过这篇博文,能让我们以后与JSON打交道时更加得心应手.由于个人水平有限 ...

  5. 写一个EF的CodeFirst的Demo

    写一个EF的CodeFirst的Demo 今天打算写一个关于EF的CodeFirs的一个小Demo.先略说一个EF的三种与数据库,怎么说,叫映射么,好吧,那就这么叫吧,就是一个是ModelFirst就 ...

  6. (转)如何基于FFMPEG和SDL写一个少于1000行代码的视频播放器

    原文地址:http://www.dranger.com/ffmpeg/ FFMPEG是一个很好的库,可以用来创建视频应用或者生成特定的工具.FFMPEG几乎为你把所有的繁重工作都做了,比如解码.编码. ...

  7. 写一个TODO App学习Flutter本地存储工具Moor

    写一个TODO App学习Flutter本地存储工具Moor Flutter的数据库存储, 官方文档: https://flutter.dev/docs/cookbook/persistence/sq ...

  8. Vue3语法快速入门以及写一个倒计时组件

    Vue3写一个倒计时组件 vue3 beta版本发布已有一段时间了,文档也大概看了一下,不过对于学一门技术,最好的方法还是实战,于是找了一个比较简单的组件用vue3来实现,参考的是vant的count ...

  9. 如何给LG gram写一个Linux下的驱动?

    其实就是实现一下几个Fn键的功能,没有标题吹得那么牛. 不知道为啥,LG gram这本子意外的小众. 就因为这个,装Linux遇到的硬件问题就没法在网上直接搜到解决办法了. Fn + F9 实现阅读模 ...

随机推荐

  1. ShowMessage和MessageDlg消息对话框(VCL)

    ShowMessage一个简单的消息提示: 例如:ShowMessage("xxxx"); MessageDlg(constAnsiString Msg, TMsgDlgType ...

  2. csuoj 1334: 好老师

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1334 1334: 好老师 Time Limit: 1 Sec  Memory Limit: 128 ...

  3. WebDriver 随笔

    在webDriver中通过 driver.findElement进行定位元素时,往往是从页面的上到下依次寻找,根据该等位方式寻找到第一个元素. driver.findElement(By.id())有 ...

  4. struts自定义拦截器

    第01步:配置web.xml,启动struts框架 <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  5. Mac配置环境变量(Java,Android,Gradle,Maven,Hosts)

    JAVA_HOME 配置环境变量 # 使用vim打开.bash_profile文件,加入java环境变量 $ vim .bash_profile export JAVA_HOME=$(/usr/lib ...

  6. ppaer 67 : matlab 函数errorbar

    MATLAB ERRORBAR 这个函数的意思是:ERRORBAR(X,Y,L,U),X是自变量,Y是因变量,L是Y的变动下限,U是Y的变动上限 errorbar(X,Y,E)  X是自变量,Y是因变 ...

  7. test generation和MBIST

    The problem of test generation Random test generation Deterministic algorithm for test generation fo ...

  8. linux下奇怪的“重名”文件

    是这样的,文件创建是通过远程命令来进行的. 就是在表单中输入命令,然后使用php的system来执行. 表单使用的是多行文本输入框. 可能某次使用 类似touch这种命令创建文件的时候多按了一次回车, ...

  9. Openstack的计算节点的nova-network异常中止及实例无法删除排错过程

    在预生产环境(172.17.46.2)发现无法删除实例,可以对实例做暂停,恢复操作. 查询原因发现计算节点的nova-network异常 [root@node-12 ~]# /etc/init.d/o ...

  10. linux主机vps简单性能测试

    第一,CPU.内存.硬盘检测 cat /proc/cpuinfo (查看CPU信息) cat /proc/meminfo (查看内存信息) df -lh (查看硬盘信息) 这个命令可以看到我们购买的V ...