<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
利用HOOK技術來做一個鍵盤記錄器,看看一天下來,我們點選了多少次鍵盤,哪些鍵的使用頻率最高。
使用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#實現簡易的鍵盤記錄器的詳細內容,更多關於C#鍵盤記錄器的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45