My keyboard hook gets called twice. 
Author Message
 My keyboard hook gets called twice.

Hi there,

It appears that my win32 keyboard hook is being called twice per
keypress. Here is my key capture file after typing "1234".

--- kb.cap ----------
11223344
Total characters: 8
---------------------

Note that total characters is 8 because the statement totalChars++ was
executed 8 times.

Is this supposed to happen?

Here is the full source code for hook.c

--------------------- hook.c -----------------
#define     STRICT      // enable strict type checking

#include <stdio.h>
#include "hook.h"

#pragma data_seg(".RIJU")
static HHOOK    hHook;          // A handle to our installed hook
static BOOL     bHookInstalled = FALSE; // TRUE if hook has been
installed
static int totalChars = 0;
#pragma comment(linker, "/section:.RIJU,RWS")

HANDLE ghDLLInst;   // Handle to the DLL's instance.  Set in DllMain.

// prototype it here since its private
LRESULT CALLBACK KeyboardHook (int nCode, WORD wParam, DWORD lParam );

//**********************************************************************
// InstallHook()
//
// Installs/Removes Filter function for the WH_KEYBOARD hook.
//
// Parameters:
//  
// BOOL bStatus   TRUE to hook, FALSE to unhook
//
// Returns:
// 1 if successful, 0 if not.
//
//**********************************************************************

int APIENTRY InstallHook (BOOL bStatus )
{
        FILE *fp;
    int nReturn = 1;

    // Make sure that we are installing/removing in the proper order

    if (bStatus == bHookInstalled)
        return 0;

    if (bStatus)
    {
        hHook = (HHOOK)SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardHook,
ghDLLInst, 0);

        if (!hHook)
            return 0;

        bHookInstalled = TRUE;
    }
    else
    {
        nReturn = UnhookWindowsHookEx(hHook);
        bHookInstalled = FALSE;
                fp = fopen("c:\\kb.cap", "a+");
                if (fp) {
                        fprintf(fp, "Total fdcharacters: %d", totalChars);
                        fclose(fp);
                }

    }  return nReturn;

Quote:
}

//**********************************************************************
//
// KeyboardHook()
//
// writes keypress to a file.
//
// Standard 3.1 KeyboardProc.  See docs for "KeyboardProc".
//
//*********************************************************************

LRESULT CALLBACK KeyboardHook (int nCode, WORD wParam, DWORD lParam )
{
        FILE *fp;
        int res = 0;
        int ascii=0;
        char KeyAscii[2];
        BYTE KeybdState[256];
        totalChars++;
    if ( nCode >= 0 )
    {
      if (GetKeyboardState (KeybdState)) {
                res = ToAscii (wParam, lParam, KeybdState, (unsigned short *)
KeyAscii, 0);
                if (res != 0) {
                        if (res == 1) ascii=(int)KeyAscii[0]; else ascii = *(int
*)KeyAscii;
                        fp = fopen("c:\\kb.cap", "a+");
                        if (fp) {
                                fputc(ascii, fp);
                                fclose(fp);
                        }
                }
          }
                // log this key press in c:\kb.cap
        }

   return (int)CallNextHookEx(hHook, nCode, wParam, lParam);

Quote:
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID
lpvReserved) {
        ghDLLInst = hinstDLL;
        return TRUE;
Quote:
}



Tue, 06 Sep 2005 19:12:11 GMT  
 My keyboard hook gets called twice.

Quote:

> It appears that my win32 keyboard hook is being called twice per
> keypress. Here is my key capture file after typing "1234".

Unfortunately, your code is so chock-full of unportable, non-ISO
extensions that it's impossible to give a good answer to your question
on comp.lang.c. I suggest that next time you post such questions, where
the problem is with the M$-Windows aspect of your program and not with
the real C aspects, or where it is so well hidden in the M$-specific
code that it is impossible to tell, to the Windows programming groups
alone.

Richard



Tue, 06 Sep 2005 19:15:08 GMT  
 My keyboard hook gets called twice.

[skipped]

Quote:
> FILE *fp;
> int res = 0;
> int ascii=0;
> char KeyAscii[2];
> BYTE KeybdState[256];
> totalChars++;
>     if ( nCode >= 0 )
>     {
>       if (GetKeyboardState (KeybdState)) {
> res = ToAscii (wParam, lParam, KeybdState, (unsigned short *)
> KeyAscii, 0);

Yes, you register both WM_KEYDOWN and UP messages. Filter them consigering
the high-order bit of KeybdState.

Regards,
Igor



Tue, 06 Sep 2005 19:35:14 GMT  
 My keyboard hook gets called twice.

Quote:



> [skipped]

> > FILE *fp;
> > int res = 0;
> > int ascii=0;
> > char KeyAscii[2];
> > BYTE KeybdState[256];
> > totalChars++;
> >     if ( nCode >= 0 )
> >     {
> >       if (GetKeyboardState (KeybdState)) {
> > res = ToAscii (wParam, lParam, KeybdState, (unsigned short *)
> > KeyAscii, 0);

> Yes, you register both WM_KEYDOWN and UP messages. Filter them consigering
> the high-order bit of KeybdState.

> Regards,
> Igor

Excellent, thank you for your reply.

JS



Wed, 07 Sep 2005 11:02:19 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. ATL Server ValidateAndExchange function getting called twice

2. ActiveX Server gets invoked twice when I call it once

3. "Apply" gets called twice

4. CAsyncSocket::OnAccept gets called twice, Why???

5. Finding out if a function gets called twice.

6. IDL gets generated twice in release mode

7. View gets drawn twice using OnHScroll

8. Creating system wide keyboard Hooks with c#

9. Keyboard Hook

10. Hooking/redirecting keyboard interrupt in C(++)

11. Keyboard hooks

12. Need Help On Win32 (Keyboard) Hooks

 

 
Powered by phpBB® Forum Software