Keyboard Hook 
Author Message
 Keyboard Hook

I am very much a beginner C programmer. I am trying to create a system-
wide keyboard hook so that a VB app I am writing will be activated
whenever the user presses a specific key. I have written the hook DLL
but it is bringing the system to a crawl. (I am using VC++ 6).

Here is the code from the .cpp file (23 lines):

#include "stdafx.h"

// Function declarations
int CALLBACK InstallHook();
LRESULT CALLBACK kbdfilter(int nCode, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK RemoveHook();

// Global variables
HHOOK hHook;                    // Holds the index to the hook as
returned by SetWindowsHookEx
HINSTANCE hInstance;    // Holds the instance of the module

// Entry point for the DLL
BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                                         )
{
    return TRUE;

Quote:
}

int CALLBACK InstallHook()
{
        // Get the instance of the module
        hInstance = GetModuleHandle("kbdhook.dll");

        // Set the system-wide hook for the keyboard
        hHook = SetWindowsHookEx(WH_KEYBOARD,
                (HOOKPROC) GetProcAddress(hInstance, "kbdfilter"),
hInstance, 0);
        return(0);

Quote:
}

LRESULT CALLBACK kbdfilter(int nCode, WPARAM wParam, LPARAM lParam)
{
        if (nCode >= 0)
        {
                // Test the app by beeping every time a key is pressed
                MessageBeep(0xFFFFFFFF);
        }

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

Quote:
}

BOOL CALLBACK RemoveHook()
{

        // Remove the keyboard hook
        return (UnhookWindowsHookEx(hHook));

Quote:
}

It has taken me ages to get this far and I realise there are probably a
lot better ways of doing this. Any suggestions?

Regards,

Mark Capaldi

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sat, 29 Jun 2002 03:00:00 GMT  
 Keyboard Hook

Some things to try:

Quote:
>#include "stdafx.h"

#pragma data_seg(".shared")
        static HINSTANCE hDLL = NULL;
        static HHOOK hHook = NULL;
#pragma data_seg()

Quote:
>// Entry point for the DLL
>BOOL APIENTRY DllMain( HANDLE hModule,
>                       DWORD  ul_reason_for_call,
>                       LPVOID lpReserved
>                                     )
>{

        hDll = hModule; // to ID this instance to InstallHook()
Quote:
>    return TRUE;
>}

/* control EXE calls (HHOOK) blah = InstallHook();
later, control EXE calls UnhookWindowsHookEx(blah) */

HHOOK InstallHook() {
        return (hHook = SetWindowsHookEx( WH_KEYBOARD,
                (HOOKPROC) kbdfilter, hDLL, 0 ) );

Quote:
}
>LRESULT CALLBACK kbdfilter(int nCode, WPARAM wParam, LPARAM lParam)
>{
>    if (nCode >= 0)

                //if ( desired key ) {
                        // take desired action
                        return 0; // don't forward key to app (probably desired)
                }

// forward keystroke to app
return(CallNextHookEx(hHook, nCode, wParam, lParam));

Quote:
>}

 - Vince
Vincent Fatica

Syracuse University Mathematics
http://barnyard.syr.edu/~vefatica/


Sat, 29 Jun 2002 03:00:00 GMT  
 Keyboard Hook
I just checked ... hHook and hDll don't have to be shared or static since
they're only used the first time.

 - Vince

Some things to try:

Quote:
>#include "stdafx.h"

#pragma data_seg(".shared")
        static HINSTANCE hDLL = NULL;
        static HHOOK hHook = NULL;
#pragma data_seg()

Quote:
>// Entry point for the DLL
>BOOL APIENTRY DllMain( HANDLE hModule,
>                       DWORD  ul_reason_for_call,
>                       LPVOID lpReserved
>                                     )
>{

        hDll = hModule; // to ID this instance to InstallHook()
Quote:
>    return TRUE;
>}

/* control EXE calls (HHOOK) blah = InstallHook();
later, control EXE calls UnhookWindowsHookEx(blah) */

HHOOK InstallHook() {
        return (hHook = SetWindowsHookEx( WH_KEYBOARD,
                (HOOKPROC) kbdfilter, hDLL, 0 ) );

Quote:
}
>LRESULT CALLBACK kbdfilter(int nCode, WPARAM wParam, LPARAM lParam)
>{
>    if (nCode >= 0)

                //if ( desired key ) {
                        // take desired action
                        return 0; // don't forward key to app (probably desired)
                }

// forward keystroke to app
return(CallNextHookEx(hHook, nCode, wParam, lParam));

Quote:
>}

 - Vince
Vincent Fatica

Syracuse University Mathematics
http://barnyard.syr.edu/~vefatica/


Sat, 29 Jun 2002 03:00:00 GMT  
 Keyboard Hook

Quote:

> I am very much a beginner C programmer. I am trying to create a system-
> wide keyboard hook so that a VB app I am writing will be activated
> whenever the user presses a specific key. I have written the hook DLL
> but it is bringing the system to a crawl. (I am using VC++ 6).

You may want to look into RegisterHotKey(). Using this function windows will
do the checking for you. You'll get a message when the key combo you seek is
pressed. At that point you can do what you like.

Regards,
Will



Sat, 29 Jun 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Creating system wide keyboard Hooks with c#

2. Keyboard Hook

3. My keyboard hook gets called twice.

4. Keyboard hooks

5. Need Help On Win32 (Keyboard) Hooks

6. Keyboard hook problem

7. Help with ToAscii function and keyboard hook

8. Interesting behavior of keyboard hook function.

9. keyboard hook question

10. PB with modeless app and global DLL keyboard hook

11. Keyboard hook, was:GetProcAddress..

12. Global Keyboard Hook Problem

 

 
Powered by phpBB® Forum Software