Funktionspointer innerhalb von Klasse

zellomat

Grünschnabel
Hallo

ich habe einen netten Quelltext gefunden, um per Windowshook Tasten abzufragen. Mein Problem steht unter diesem Quelltext.

Code:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
// function declaration.
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam );
LRESULT CALLBACK LowLevelMouseProc( int nCode, WPARAM wParam, LPARAM lParam );
int main()
{
    // Retrieve the applications instance
    HINSTANCE appInstance = GetModuleHandle(NULL);
    // Set a global Windows Hook to capture keystrokes.
    SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, appInstance, 0 );
    SetWindowsHookEx( WH_MOUSE_LL, LowLevelMouseProc, appInstance, 0 );
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0)


    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }


    return 0;
}
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )


{
    // Declare our pointer to the KBDLLHOOKSTRUCT
    KBDLLHOOKSTRUCT *pKeyBoard = (KBDLLHOOKSTRUCT *)lParam;


            printf("time: %u, vkCode: %d, wParam: %d \n", pKeyBoard->time, pKeyBoard->vkCode, wParam ); // Show us when the key has been pushed



        //return CallNextHookEx( NULL, nCode, wParam, lParam );

    return 0;
}

Dies wollte ich nun in eine Klasse bringen. Z.B. wie folgt:

Code:
class test {
public:
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam ) {...}

LRESULT CALLBACK LowLevelMouseProc( int nCode, WPARAM wParam, LPARAM lParam ) {...}

test() {
    HINSTANCE appInstance = GetModuleHandle(NULL);
    SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, appInstance, 0 );
    SetWindowsHookEx( WH_MOUSE_LL, LowLevelMouseProc, appInstance, 0 );
    MSG msg;
    GetMessage(&msg, NULL, 0, 0);
}
};

Aber Probleme machen folgende Zeilen:

Code:
SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, appInstance, 0 );
SetWindowsHookEx( WH_MOUSE_LL, LowLevelMouseProc, appInstance, 0 );

Scheinbar werden Pointer statischer Funktionen erwartet. Ich hab einige Variationen ausprobiert, nichts half. Gibt es hierfür eine Lösung ohne alle Funktionen nach außerhalb der Klasse zu verlagern?
 
Hi.

Es gibt keine Lösung dafür, du mußt entweder normale Funktionen definieren oder statische Methoden verwenden.

Du könntest höchstens eine Klasse erstellen, die statische Methoden besitzt welche als Hook-Funktionen gesetzt sind und bei bei der sich andere Objekte registrieren können. Alle registrierten Objekte müssten dann benachrichtig werden falls die entsprechende Hook-Callback-Funktion aufgerufen wird.

Gruß
 
Zurück