日期:2014-05-18  浏览次数:21127 次

分享-C#简单改键工具源码
今天研究全局钩子,参考一些资料,写了一个C#改键工具,只改一个键,现在把源码贴出来,大家有兴趣的可以多加几个键。求高手指正。只用福昕阅读器测试过。测试通过了。

下载地址http://download.csdn.net/detail/ooo7880000/4187121。免积分下载

C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ChangeKeys
{
    /// <summary>
    /// 改键工具
    /// <Author>YuSir</Author>
    /// </summary>
    public partial class ChangeKeys : Form
    {
        #region 构造

        /// <summary>
        /// 构造函数
        /// </summary>
        public ChangeKeys()
        {
            //绘制界面上控件等
            InitializeComponent();
            //设置焦点为输入按键文本框
            TxtOldKey.Focus();
        }

        #endregion
       
        #region 变量

        /// <summary>
        /// 声明委托
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);

        /// <summary>
        /// 声明键盘钩子事件类型
        /// </summary>
        HookProc KeyboardHookProcedure;

        /// <summary>
        /// 键盘钩子句柄
        /// </summary>
        static int hKeyboardHook = 0;
        
        //装置钩子的函数 
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

        //卸下钩子的函数 
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        //获取某个进程的句柄函数
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        /// <summary>
        /// 普通按键消息
        /// </summary>
        private const int WM_KEYDOWN = 0x100;
        /// <summary>
        /// 系统按键消息
        /// </summary>
        private const int WM_SYSKEYDOWN = 0x104;

        //鼠标常量 
        public const int WH_KEYBOARD_LL = 13;

        //声明键盘钩子的封送结构类型 
        [StructLayout(LayoutKind.Sequential)]
        public class KeyboardHookStruct
        {
            public int vkCode; //表示一个在1到254间的虚似键盘码 
            public int scanCode; //表示硬件扫描码 
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

        #endregion
        
        #region 事件

        /// <summary>
        /// 释放按键时把按键显示在文本框中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtOldKey_KeyUp(object sender, KeyEventArgs e)
        {
            TxtOldKey.Text = e.KeyCode.ToString();
        }

        /// <summary>
        /// 释放按键时把按键显示在文本框中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtNewKey_KeyUp(object sender, KeyEventArgs e)
        {
            TxtNewKey.Text = e.KeyCode.ToString();
        }

        /// <summary>
        /// 启动键盘钩子
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnStart_Click(object sender, EventArgs e)
        {