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

改键是一种习惯,比如在玩儿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. C#: 集合

    摘自http://www.cnblogs.com/kissdodog/archive/2013/01/29/2882195.html 先来了解下集合的基本信息 1.BCL中集合类型分为泛型集合与非泛型 ...

  2. .NET 4.0 MemoryCache with SqlChangeMonitor

    Summary There isn't a lot of documentation on the internet about how to use the SqlChangeMonitor wit ...

  3. VS2013中的快捷键

    在VS2012中Ctrl+E+D 是对齐代码,然而在VS2013中变成了Ctrl+K+F #region的快捷键 Ctrl+K+S /// <summary> 快捷键 先写好函数或方法,然 ...

  4. :nth-child()和:nth-of-type(n)区别

    ele:nth-child(n) 父元素下第n个元素且这个元素为ele ele:nth-of-type(n) 指父元素下第n个ele元素

  5. 夺命雷公狗---DEDECMS----33dedecms自定义搜索以及分页功能完成

    我们现在要开始实现模版里面的搜索功能了,我们先找要做出一个检索提交表单,如下所示: 只要我们点击生成之后我们的表单就获取到了,可以直接拿生成好的html表单拿来用来测试下.. 将他嵌入首页的模版文件, ...

  6. windows下pip升级到8.1.2

    升级pip只要切换到easy_install-3.5目录下:  easy_install-3.5 pip==8.1.2

  7. 8. 星际争霸之php设计模式--享元模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  8. c语言对文件操作完成后尽量手动关闭

    是这样的,我写了一个函数,传给函数文件名,在函数中对文件写入一些内容.在这个函数的后面没有手动使用 fclose. 当在程序中对这个函数调用两次之后,最终把要写入的文件写错了. 在第二次使用 fope ...

  9. 「thunar」给thunar增加搜索文件功能

    1.安装catfish sudo apt-get install catfish 2.配置thunar,添加[自定义动作] 打开 Thunar 后,点击 Edit -> Configure cu ...

  10. 161020、web调试工具fiddler介绍及使用

    简介: Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的"进出"Fiddler的数据(指cookie,ht ...