Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use DirectX::Keyboard::ProcessMessage with SetWindowsHookEx (for low-level global keyboard / WH_KEYBOARD_LL) #46

Closed
rogueyoshi opened this issue Jun 23, 2016 · 4 comments
Labels
input Related to GamePad, Keyboard, and Mouse question

Comments

@rogueyoshi
Copy link

rogueyoshi commented Jun 23, 2016

The hook succeeds and messages get sent correctly when keys are pressed, but does it somehow get processed differently in ProcessMessage than a normal messaging thread?

// Create messenging thread.
m_pHookThread = new std::thread(&CDirectXWrapper::Hook, this);
m_pHookThread->detach();

void CDirectXWrapper::Hook()
{
    // Install hooks
    m_hHook = SetWindowsHookEx(WH_KEYBOARD_LL, [](const int nCode, const WPARAM wParam, const LPARAM lParam) {
        switch (wParam)
        {
        case WM_ACTIVATEAPP:
            Keyboard::ProcessMessage(nCode, wParam, lParam);
            break;
        case WM_KEYDOWN:
        case WM_SYSKEYDOWN:
        case WM_KEYUP:
        case WM_SYSKEYUP:
            Keyboard::ProcessMessage(nCode, wParam, lParam);
            break;
        }

        return CallNextHookEx(m_hHook, nCode, wParam, lParam);
    }, NULL, 0);

    if (m_hHook == NULL)
    {
        std::wcout << "Keyboard hook failed!" << std::endl;
    }
    else
    {
        std::wcout << "Keyboard hooked!" << std::endl;
    }

    // Messenging loop.
    while (GetMessage(NULL, NULL, 0, 0));
}

@walbourn
Copy link
Member

Why use a 'hook' here instead of just putting it into the application's main WndProc?

@rogueyoshi
Copy link
Author

rogueyoshi commented Jun 23, 2016

The application is actually a DirectShow Source Filter DLL which uses DX11 (with no reference to a usable WndProc) rather than a standalone app. The global hook is needed so that hotkeys can be detected regardless of program focus.

@rogueyoshi
Copy link
Author

Also, I should state that any key queryid after the setup is detected as false.

auto kb = m_pDirectXWrapper->GetKeyboardState();
//kb.A for example, would return false even if held.

@walbourn
Copy link
Member

I'd recommend against using the DirectX Tool Kit Keyboard and Mouse class in a hook scenario. It's just not the intended use case.

@walbourn walbourn added the input Related to GamePad, Keyboard, and Mouse label Mar 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
input Related to GamePad, Keyboard, and Mouse question
Projects
None yet
Development

No branches or pull requests

2 participants