• 大小: 37.31 KB
    文件类型: .rar
    金币: 1
    下载: 1 次
    发布日期: 2024-09-02
  • 语言: C#
  • 标签: 钩子  键盘消息  c#  

资源简介

c#钩子获取键盘消息

这个是别人写的,我拿来试了试,虽然依然不是很明白,但既然已实现功能,就放在这里吧。

资源截图

代码片段和文件信息

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

namespace HOOKCSharpTest
{
    public partial class Form1 : Form
    {
        static KeyboardHook kbh;

        public Form1()
        {
            InitializeComponent();
        }

        public void SetText(char c)
        {
            richTextBox1.Text += c;
        }


        /// 
        /// 应用程序的主入口点。
        /// 

        [STAThread]
        static void Main()
        {
            Application.EnableVisualstyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 frm = new Form1();
            using (kbh = new KeyboardHook(frm))
            {
                Application.Run(frm);
            }
        }
    }

    public class KeyboardHook : IDisposable
    {
        [DllImport(“user32.dll“ CharSet = CharSet.Auto SetLastError = true)]
        public static extern IntPtr SetWindowsHookExW(
            int idHook 
            HookHandlerDelegate lpfn 
            IntPtr hmod 
            uint dwThreadID);

        [DllImport(“user32.dll“ CharSet = CharSet.Auto SetLastError = true)]
        public static extern IntPtr CallNextHookEx(
            IntPtr idHook
            int nCode
            IntPtr wparam
            ref KBDLLHOOKSTRUCT lparam);

        [DllImport(“user32.dll“ CharSet = CharSet.Auto SetLastError = true)]
        public static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport(“kernel32.dll“ CharSet = CharSet.Auto SetLastError = true)]
        public static extern IntPtr GetModuleHandle(String modulename);

        public const int WM_KEYDOWN = 0x0100;
        public const int WH_KEYBOARD_LL = 13;
        public const int WM_SYSKEYDOWN = 0x0104;
        public const int WH_MOUSE_LL = 14;


        private HookHandlerDelegate proc;
        private IntPtr hookID = IntPtr.Zero;


        private Form1 m_F;
        public KeyboardHook(Form1 frm)
        {
            m_F = frm;
            proc = new HookHandlerDelegate(HookCallback);
            using (Process curPro = Process.GetCurrentProcess())
            using (ProcessModule curMod = curPro.MainModule)
            {
                hookID = SetWindowsHookExW(WH_KEYBOARD_LL
                    proc
                    GetModuleHandle(curMod.ModuleName)
                    0);
            }
        }

        private IntPtr HookCallback(
            int nCode
            IntPtr wparam
            ref KBDLLHOOKSTRUCT lparam)
        {
             if (
                nCode >= 0
                &&
                (wparam == (IntPtr)WM_KEYDOWN
                ||
                wparam == (IntPtr)WM_SYSKEYDOWN)
          

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1402  2009-04-15 15:12  HOOKCSharpTest\HOOKCSharpTest.sln

    ..A..H.     11776  2009-04-15 15:48  HOOKCSharpTest\HOOKCSharpTest.suo

     文件       1953  2009-04-15 15:12  HOOKCSharpTest\Key\KeyHook.csproj

     文件        180  2009-04-15 14:58  HOOKCSharpTest\Key\keyboard.cs

     文件        134  2009-04-15 16:09  HOOKCSharpTest\Key\obj\KeyHook.csproj.FileList.txt

     文件       7680  2009-04-15 15:12  HOOKCSharpTest\Key\obj\Debug\KeyHook.pdb

     文件      16384  2009-04-15 15:12  HOOKCSharpTest\Key\obj\Debug\KeyHook.dll

     文件      16384  2009-04-15 15:12  HOOKCSharpTest\Key\bin\Debug\KeyHook.dll

     文件       7680  2009-04-15 15:12  HOOKCSharpTest\Key\bin\Debug\KeyHook.pdb

     文件       1337  2009-04-15 14:44  HOOKCSharpTest\Key\Properties\AssemblyInfo.cs

     文件       3197  2009-04-15 15:48  HOOKCSharpTest\HOOKCSharpTest\HOOKCSharpTest.csproj

     文件       1944  2009-04-15 15:48  HOOKCSharpTest\HOOKCSharpTest\Form1.Designer.cs

     文件       5814  2009-04-15 15:48  HOOKCSharpTest\HOOKCSharpTest\Form1.resx

     文件       3665  2009-04-15 16:09  HOOKCSharpTest\HOOKCSharpTest\Form1.cs

     文件        317  2009-04-15 16:09  HOOKCSharpTest\HOOKCSharpTest\obj\HOOKCSharpTest.csproj.FileList.txt

     文件        180  2009-04-15 15:12  HOOKCSharpTest\HOOKCSharpTest\obj\Debug\HOOKCSharpTest.Properties.Resources.resources

     文件      20480  2009-04-15 16:09  HOOKCSharpTest\HOOKCSharpTest\obj\Debug\HOOKCSharpTest.exe

     文件        180  2009-04-15 15:48  HOOKCSharpTest\HOOKCSharpTest\obj\Debug\HOOKCSharpTest.Form1.resources

     文件        842  2009-04-15 15:48  HOOKCSharpTest\HOOKCSharpTest\obj\Debug\HOOKCSharpTest.csproj.GenerateResource.Cache

     文件      22016  2009-04-15 16:09  HOOKCSharpTest\HOOKCSharpTest\obj\Debug\HOOKCSharpTest.pdb

     文件       5632  2005-11-11 22:25  HOOKCSharpTest\HOOKCSharpTest\bin\Debug\HOOKCSharpTest.vshost.exe

     文件      20480  2009-04-15 16:09  HOOKCSharpTest\HOOKCSharpTest\bin\Debug\HOOKCSharpTest.exe

     文件      22016  2009-04-15 16:09  HOOKCSharpTest\HOOKCSharpTest\bin\Debug\HOOKCSharpTest.pdb

     文件       5612  2009-04-15 14:42  HOOKCSharpTest\HOOKCSharpTest\Properties\Resources.resx

     文件        249  2009-04-15 14:42  HOOKCSharpTest\HOOKCSharpTest\Properties\Settings.settings

     文件       1212  2009-04-15 14:42  HOOKCSharpTest\HOOKCSharpTest\Properties\AssemblyInfo.cs

     文件       2882  2009-04-15 14:42  HOOKCSharpTest\HOOKCSharpTest\Properties\Resources.Designer.cs

     文件       1097  2009-04-15 14:42  HOOKCSharpTest\HOOKCSharpTest\Properties\Settings.Designer.cs

     目录          0  2009-04-15 16:10  HOOKCSharpTest\Key\obj\Debug\Refactor

     目录          0  2009-04-15 16:10  HOOKCSharpTest\Key\obj\Debug\TempPE

............此处省略17个文件信息

评论

共有 条评论