2

I have the following method which is attempting to intercept a global press of ALT + Z:

private int KbHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode >= 0)
    {
        var hookStruct = (KbLLHookStruct)Marshal.PtrToStructure(lParam, typeof(KbLLHookStruct));

        bool altDown = GetKeyState(VK_ALT) != 0;

        if (altDown) 
        {
            if(hookStruct.vkCode == 0x5A) //alt + z
            {
                MessageBox.Show("ALT + Z"); 
            }
        }
    }

    return CallNextHookEx(_hookHandle, nCode, wParam, lParam);
}

This method almost works, however the message "ALT + Z" is shown twice every time I enter the key command.

Initially, I thought that I was capturing the event twice, once when Z was pressed and once when Z was released. I tried modifying the code to check the wParam variable to see if the event was a key up or down event.

public const int WM_KEYDOWN = 0x0100;

...

if(hookStruct.vkCode == 0x5A && wParam == (IntPtr)WM_KEYDOWN) //alt + z
{
    MessageBox.Show("ALT + Z"); 
}

However, in this case no message is displayed when I enter the key command.

What am I missing here?

James Hogle
  • 3,010
  • 3
  • 22
  • 46

0 Answers0