-1

here is what I've done so far trying to learn to deal with hooks, buttons and messages on windows:

in main.cpp   

    //Create a thread for keyboard:

        HANDLE hScreenThread2;

        DWORD idScreenThread2;

        hScreenThread2 = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) KeyLogger, 0, 0, &idScreenThread2);

         if(hScreenThread2) {
         WaitForSingleObject(hScreenThread2, INFINITE);   
         TerminateThread(hScreenThread2, 0);                   
         CloseHandle(hScreenThread2);                                
    }

    in keylogger.cpp

        DLLEXPORT DWORD WINAPI KeyLogger ( LPVOID lpParamenter )
        {
                                         //armazena a mensagem do windows
            HINSTANCE hExe = GetModuleHandle(NULL);  //?
            //cria o hook
            hHook1 = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) KeyBoardHook, hExe, 0);
            //loop de mensagens
            MessageLoop();
            //destrói o hook
            UnhookWindowsHookEx(hHook1);
            return 0;
        }

The message loop

    VOID MessageLoop()
    {
        MSG msg;
        while(GetMessage(&msg, NULL, 0, 0))
        {
           TranslateMessage(&msg);
           DispatchMessage(&msg);
        }     

    }

and finally the source of my headache:

    LRESULT CALLBACK KeyBoardHook( int nCode, WPARAM wParam, LPARAM lParam )

    {   
        if((nCode == HC_ACTION) && (wParam == WM_KEYDOWN))
        {
            wchar_t c;
            switch (wParam)
            {
            case WM_CHAR:
                c = (wchar_t)wParam;
                break;
            default:
                break;
            }
        }
        return CallNextHookEx(hHook1,nCode,wParam,lParam);
    }

In short: I can not capture keystrokes and much less the keys that must be combined to form accented letters on my keyboard for example I have to press the acute accent (') drop and press the letter (e) for to have the letter "é". My keyboard is ABNT2 Brazilian Portuguese. Any idea where to start because I've tried using GetAsyncKeyState () but as said before that I have a letter "é" I have to first press the key ('), release it and then press down another key and I dont know how to make for capture two events separate. I've tried GetKeyState (0) along with GetKeyboardState (kb) (kb is a BYTE buffer with space of 256).

Thanks for any help or ideia.

1 Answers1

2
if((nCode == HC_ACTION) && (wParam == WM_KEYDOWN))
{
    wchar_t c;
    switch (wParam)
    {
    case WM_CHAR:
        c = (wchar_t)wParam;
        break;
    default:
        break;
    }
}

The if condition will only evaluate true when wParam == WM_KEYDOWN. At which point there is absolutely no possibility for wParam to simultaneously be WM_CHAR. So you can never assign a value to c.

The documentation explains that for WH_KEYBOARD_LL hooks, the possible values for wParam are WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP. In other words, you won't be getting any WM_CHAR messages. You'll need to translate the key strokes yourself. These questions cover that topic:

In summary, it's easy enough for simple keyboard layouts, but very tricky as soon as an IME is involved.

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490