Original Post
I've been messing around with windows hooks, I find them interesting, and so I'm trying to make a nice complex little program out of them. Problem though, I'm stuck already. I'm having problems with printing the correct character to the console(wether it be an "l", capslock, or any other character, except enter and space). I don't see what's wrong, could someone check through quickly and spot anything that would print quotation marks instead of the correct character. Yes, I said quotation marks. Instead of printing an 'h' if I press "h", it prints a quotation mark to the screen, very frustrating. Here's what I have so far. EDIT: Also, Whenever a key is pressed, it outputs to the screen 2 characters(one for keydown, one for keyup). Do you know how I would discard the keyup's aswell?
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include <string>
HHOOK myHook;
LRESULT LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
std::string ch;
if(((DWORD)lParam & 0x40000000 || (HC_ACTION==nCode)))
{
KBDLLHOOKSTRUCT *HookStruct = (KBDLLHOOKSTRUCT *)lParam;
if(HookStruct->vkCode == VK_SPACE)
{
std::cout << " ";
return FALSE;
}
else if(HookStruct->vkCode == VK_RETURN)
{
std::cout << std::endl;
return FALSE;
}
else
{
BYTE ks[256];
GetKeyboardState(ks);
WORD w;
UINT scan;
scan=0;
ToAscii(wParam, scan, ks, &w, 0);
std::cout << char(w);
}
}
return CallNextHookEx(myHook, nCode, wParam, lParam);
}
void InstallHook(void)
{
if(myHook)
return;
myHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC )LowLevelKeyboardProc, GetModuleHandle(NULL), 0);
if(!myHook)
{
std::cout << "Couldn't install hook for some reason";
}
}
void KillHook(void)
{
if(myHook)
UnhookWindowsHookEx(myHook);
myHook = 0;
}
int main()
{
InstallHook();
MessageBox(NULL, "Hit ok to kill the hook and close program", "Hook box", MB_OK);
KillHook();
return 0;
}