none
Hooking Mouse/Keyboard Problem RRS feed

  • Question

  • I have a program the hooks the mouse. It works OK in release mode but if I run with the debugger I get the error message shown below. So I checked the Internet and tried a few thing to no avail.

    I found similar code at:
    https://www.experts-exchange.com/questions/26954096/Keyboard-Hook-Class-x64-Issue.html

    Except it hooked the keyboard. Did not look like it would fix it but I copied it and tried it anyway. Same problem. There are two version there - I tried them both. But the entire class is there if anyone wants to see it.

    The following error occurs at the line: Return CallNextHookEx...

    Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\Coder\Documents\Visual Studio 2015\Projects\Suite\Project Suite\bin\x86\Debug\Suite.exe'.

    Private Shared Function KeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
         
            If (nCode = HC_ACTION) Then
                Select Case wParam
                    Case WM_KEYDOWN, WM_SYSKEYDOWN
                        RaiseEvent KeyDown(CType(lParam.vkCode, Keys))
                    Case WM_KEYUP, WM_SYSKEYUP
                        RaiseEvent KeyUp(CType(lParam.vkCode, Keys))
                End Select
            End If
            Return CallNextHookEx(KeyHook, nCode, wParam, lParam)
        End Function

    I believe lParam should be an Intptr which would then points to a KBDLLHOOKSTRUCT structure being giving to the function. I think something like the following is needed:

       'tagKBDLLHOOKSTRUCT *str = (tagKBDLLHOOKSTRUCT *)lParam
    Anybody know how to do the equilent in VB.NET?

    Or have other helpful suggestios!

    I found the above in:
    LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if (nCode < 0)
        return CallNextHookEx(NULL, nCode, wParam, lParam);
    tagKBDLLHOOKSTRUCT *str = (tagKBDLLHOOKSTRUCT *)lParam;
    switch(str->flags)
    {
        case (LLKHF_ALTDOWN):
            qDebug() << "ALT";
            delete str;
        return 1;
    }
    if (wParam == WM_KEYDOWN)
    {
        switch (str->vkCode)
        {
            case VK_RWIN:
            case VK_LWIN:
            case VK_LCONTROL:
            case VK_RCONTROL:
            case VK_APPS:
            case VK_SLEEP:
            case VK_MENU:
                qDebug() << "SPECIAL PRESS";
                delete str;
            return 1;
        }
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
    }
    at:
    https://stackoverflow.com/questions/11976708/how-to-configure-keyboardproc-to-avoid-keystrokes
    • Moved by 宝宝徐 Thursday, October 12, 2017 2:58 AM
    Wednesday, October 11, 2017 10:06 PM

Answers

  • I believe lParam should be an Intptr which would then points to a KBDLLHOOKSTRUCT structure being giving to the function. I think something like the following is needed:

    'tagKBDLLHOOKSTRUCT *str = (tagKBDLLHOOKSTRUCT *)lParam

    See the sample I just posted in another thread : How to hook number keys and character keys only?

    (tested with VB.NET 2015 on Windows 10)


    • Edited by Castorix31 Thursday, October 12, 2017 4:53 AM
    • Marked as answer by scholarQ Thursday, October 12, 2017 7:59 AM
    Thursday, October 12, 2017 4:51 AM

All replies

  • Hi,

    I will move your thread to VB forum for professional support, thank you for understanding. If you have problem about visual studio IDE, please feel free to let me know.

    Best regards,

    Joyce


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 12, 2017 2:56 AM
  • I believe lParam should be an Intptr which would then points to a KBDLLHOOKSTRUCT structure being giving to the function. I think something like the following is needed:

    'tagKBDLLHOOKSTRUCT *str = (tagKBDLLHOOKSTRUCT *)lParam

    See the sample I just posted in another thread : How to hook number keys and character keys only?

    (tested with VB.NET 2015 on Windows 10)


    • Edited by Castorix31 Thursday, October 12, 2017 4:53 AM
    • Marked as answer by scholarQ Thursday, October 12, 2017 7:59 AM
    Thursday, October 12, 2017 4:51 AM