Skip to main content
GameDev.net gamedev.net
🔒 Locked

Hook problem

Started by cherryhouse Apr 14, 2006 at 5:33 PM 1 replies 850+ views
Original Post
cherryhouse
cherryhouse
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.
#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;
}


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?
Hello?
Extrarius
Extrarius
Use LLKHF_UP with the flags member of KBDLLHOOKSTRUCT to test whether the key is being released or pressed. You can also use the wParam member of the LowLevelKeyboardProc function.

As far as getting quotes, try passing the "scanCode" member of the struct to ToAscii instead of the constant zero (scan is always zero when you call ToAscii).

MSDN is most helpful
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
cherryhouse
cherryhouse
MSDN is most helpful, it reminded me I could simply use wParam to determine wether or not the key is pressed or not. Also, I just decided to use a for loop to let me know which key is pressed then use std::cout << char(i) to let me know which key is pressed. Works out alright, except a few problems that will be covered once I get down to them. Thanks anyways though.
Hello?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.