当前位置:   article > 正文

C# 键盘记录器

c# 键盘记录

        利用HOOK技术来做一个键盘记录器,看看一天下来,我们点击了多少次键盘,哪些键的使用频率最高。

实现功能:

使用C#实现一个键盘记录器

开发环境:

开发工具: Visual Studio 2013

.NET Framework版本:4.5

实现代码:

  1. public class HookUtil
  2. {
  3. #region windows api
  4. /// <summary>
  5. /// 安装钩子
  6. /// </summary>
  7. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  8. public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
  9. public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
  10. /// <summary>
  11. /// 继续下一个钩子
  12. /// </summary>
  13. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  14. public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
  15. /// <summary>
  16. /// 卸载钩子
  17. /// </summary>
  18. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  19. public static extern bool UnhookWindowsHookEx(int idHook);
  20. /// <summary>
  21. ///获取当前线程编号(线程钩子需要用到)
  22. [DllImport("kernel32.dll")]
  23. static extern int GetCurrentThreadId();
  24. /// <summary>
  25. /// 获取当前实例的函数
  26. /// </summary>
  27. [DllImport("kernel32.dll")]
  28. public static extern IntPtr GetModuleHandle(string name);
  29. /// <summary>
  30. /// 获取按键的状态
  31. /// </summary>
  32. /// <param name="pbKeyState"></param>
  33. /// <returns></returns>
  34. [DllImport("user32")]
  35. public static extern int GetKeyboardState(byte[] pbKeyState);
  36. /// <summary>
  37. /// 将指定的虚拟键码和键盘状态翻译为相应的字符或字符串
  38. /// </summary>
  39. [DllImport("user32")]
  40. public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState,byte[] lpwTransKey,int fuState);
  41. #endregion
  42. /// <summary>
  43. /// 键盘结构
  44. /// </summary>
  45. [StructLayout(LayoutKind.Sequential)]
  46. public class KeyboardHookStruct
  47. {
  48. public int vkCode; //定一个虚拟键码。该代码必须有一个价值的范围1至254
  49. public int scanCode; // 指定的硬件扫描码的关键
  50. public int flags; // 键标志
  51. public int time; // 指定的时间戳记的这个讯息
  52. public int dwExtraInfo; // 指定额外信息相关的信息
  53. }
  54. //定义为键盘钩子
  55. public int WH_KEYBOARD_LL = 13;
  56. //相关键盘事件
  57. public event KeyEventHandler KeyDownEvent;
  58. public event KeyPressEventHandler KeyPressEvent;
  59. public event KeyEventHandler KeyUpEvent;
  60. //相关动作
  61. private const int WM_KEYDOWN = 0x100;//KEYDOWN
  62. private const int WM_KEYUP = 0x101;//KEYUP
  63. private const int WM_SYSKEYDOWN = 0x104;//SYSKEYDOWN
  64. private const int WM_SYSKEYUP = 0x105;//SYSKEYUP
  65. //hookid
  66. private int hookID = 0;
  67. //向下传递数据
  68. public Keys NoNextKeyCode;
  69. /// <summary>
  70. /// 安装钩子
  71. /// </summary>
  72. public void StartHook()
  73. {
  74. if (hookID == 0)
  75. {
  76. HookProc hookProc = new HookProc(KeyboardHookProc);
  77. hookID = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName), 0);
  78. if (hookID == 0)
  79. {
  80. StopHook();
  81. throw new Exception("安装键盘钩子失败");
  82. }
  83. }
  84. }
  85. public void StopHook()
  86. {
  87. bool isStop = true;
  88. if (hookID != 0)
  89. {
  90. isStop = UnhookWindowsHookEx(hookID);
  91. hookID = 0;
  92. }
  93. if (!isStop) throw new Exception("卸载键盘钩子失败!");
  94. }
  95. /// <summary>
  96. /// 监听事件
  97. /// </summary>
  98. private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
  99. {
  100. if ((nCode >= 0) && (KeyDownEvent != null || KeyUpEvent != null || KeyPressEvent != null))
  101. {
  102. KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
  103. //按下处理
  104. if (KeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
  105. {
  106. Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
  107. KeyEventArgs e = new KeyEventArgs(keyData);
  108. KeyDownEvent(this, e);
  109. //阻止向下传递
  110. if (NoNextKeyCode == keyData)
  111. {
  112. return hookID;
  113. }
  114. }
  115. //按下并抬起处理
  116. if (KeyPressEvent != null && wParam == WM_KEYDOWN)
  117. {
  118. byte[] keyState = new byte[256];
  119. GetKeyboardState(keyState);
  120. byte[] inBuffer = new byte[2];
  121. if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1)
  122. {
  123. KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);
  124. KeyPressEvent(this, e);
  125. }
  126. }
  127. // 抬起处理
  128. if (KeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
  129. {
  130. Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
  131. KeyEventArgs e = new KeyEventArgs(keyData);
  132. KeyUpEvent(this, e);
  133. }
  134. }
  135. return CallNextHookEx(hookID, nCode, wParam, lParam);
  136. }
  137. ~HookUtil()
  138. {
  139. StopHook();
  140. }
  141. }
  1. HookUtil keyHook = new HookUtil();
  2. private void btnBegin_Click(object sender, EventArgs e)
  3. {
  4. keyHook.KeyDownEvent += new KeyEventHandler(hook_KeyDown);//钩住按下事件
  5. keyHook.StartHook();
  6. btnBegin.Enabled = false;
  7. btnEnd.Enabled = true;
  8. }
  9. private void btnEnd_Click(object sender, EventArgs e)
  10. {
  11. keyHook.StopHook();
  12. btnBegin.Enabled = true;
  13. btnEnd.Enabled = false;
  14. }
  15. private void btnInfo_Click(object sender, EventArgs e)
  16. {
  17. string path = System.AppDomain.CurrentDomain.BaseDirectory + "log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
  18. if (!File.Exists(path))
  19. {
  20. MessageBox.Show("还未监听到数据,请操作后再查看");
  21. return;
  22. }
  23. var list = File.ReadAllLines(path).ToList().GroupBy(s => s).Select(s => new { s.Key, s.ToList().Count }).OrderByDescending(s => s.Count);
  24. FrmInfo frm = new FrmInfo();
  25. frm.Show();
  26. foreach (var item in list)
  27. {
  28. frm.addItems(new string[] { "", item.Key, item.Count + "" });
  29. }
  30. }
  31. private void hook_KeyDown(object sender, KeyEventArgs e)
  32. {
  33. if (!listKey.Contains(e.KeyData))
  34. {
  35. if (Control.ModifierKeys != Keys.None)
  36. {
  37. WriteLog(Control.ModifierKeys + "+" + e.KeyData);
  38. }
  39. else
  40. {
  41. WriteLog(e.KeyData + "");
  42. }
  43. }
  44. else
  45. {
  46. WriteLog(e.KeyData + "");
  47. }
  48. }

实现效果:

由简入繁,拿来即用

更多精彩,请搜索公众号:Csharp 小记

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/272446?site
推荐阅读
相关标签
  

闽ICP备14008679号