-2

I want to use the Windows API to get keyboard input from a window. From what I have heard, I can create a callback function that will be called when some events happen. I just don't know how.

ModuleManager::ModuleManager() {
    HWND getGameWindow = FindWindow(NULL, TEXT("GameName"));
    wndProc = (WNDPROC)SetWindowLongPtrA(getGameWindow, GWLP_WNDPROC, 0);
}

LRESULT CALLBACK ModuleManager::WndProc(const HWND hwnd, unsigned int message, uintptr_t wParam, long lparam) {
    if (message == WM_CHAR) {
        for (Module* m : modules) {
            if (m->getKeybinding() == wParam) {
                m->isActive = !m->isActive; // toggle
            }
        }
    }

    return CallWindowProcA(wndProc, hwnd, message, wParam, lparam);
}

Here is my current code. I want to set a callback on WndProc function.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

0

It seems i figured it out. The callback function must be a static function for some reason. So the correct code is following:

ModuleManager::ModuleManager() {
    HWND getGameWindow = FindWindow(NULL, TEXT("GameName"));
    wndProc = (WNDPROC)SetWindowLongPtrA(getGameWindow, GWLP_WNDPROC, (LONG_PTR) &ModuleManager::WndProc);
}

LRESULT CALLBACK ModuleManager::WndProc(const HWND hwnd, unsigned int message, uintptr_t wParam, long lparam) {
    if (message == WM_CHAR) {
        for (Module* m : modules) {
            if (m->getKeybinding() == wParam) {
                m->isActive = !m->isActive; // toggle
            }
        }
    }

    return CallWindowProcA(wndProc, hwnd, message, wParam, lparam);
}