1

The utility I am writing requires use of the SetWindowsHookEx function to capture key presses with the LowLevelKeyboardProc callback.

I have implemented the code found on this MSDN blog.

The correct keycode is being captured, however I appear to be unable to get the correct value of the 30th bit when using the bitmask taken from the answer to this StackOverflow question: Get 30th bit of the lParam param in WM_KEYDOWN message

private const int LAST_KEY_STATE_BITMASK = 0x40000000;

This bit should allow me to detect if the key is being held down, according to the Microsoft documentation.

I only want to act upon a keydown event when the key is first pressed and is not being held; keyup won't suffice.

The code I am using to try and get the value of the 30th bit is:

    int vkCode = Marshal.ReadInt32(lParam);
    int lastKeyState = (vkCode & LAST_KEY_STATE_BITMASK);

Which, from my understanding, should return 0 when the key isn't being held down and 1 when it is.

I have also tried using lParam.ToInt32() and (int)lParam in place of vkCode but to no avail.

Please help!

Here's the full program as it is in its current state:

using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class KeyCapture
{
    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private const int LAST_KEY_STATE_BITMASK = 0x40000000;

    private static LowLevelKeyboardProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;

    public static void Main()
    {
        _hookID = SetHook(_proc);
        Application.Run();
        UnhookWindowsHookEx(_hookID);
    }

    private static IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private delegate IntPtr LowLevelKeyboardProc(
        int nCode, IntPtr wParam, IntPtr lParam);

    private static IntPtr HookCallback(
        int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);
            int lastKeyState = (vkCode & LAST_KEY_STATE_BITMASK);

            Console.WriteLine((Keys)vkCode + "|" + vkCode + "|" + lastKeyState);
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook,
        LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
        IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);
}
Community
  • 1
  • 1
cpoliver
  • 65
  • 5
  • 1
    [Read the documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644985.aspx). You seem to assume that `lParam` passed to `LowLevelKeyboardProc` follows the same convention as `lParam` passed to `WM_KEYDOWN` message. This is not the case: in `LowLevelKeyboardProc`, `lParam` is "a pointer to a `KBDLLHOOKSTRUCT` structure." – Igor Tandetnik Jan 06 '14 at 21:13
  • @IgorTandetnik Well now I feel silly! After a day of staring at C# with meaningful variable names, I must have skimmed over that. Thanks ever so much for the quick reply. – cpoliver Jan 06 '14 at 21:30
  • @IgorTandetnik please forgive my ignorance (I have been searching relentlessly since your reply) but can you shed any light on how I can read the lParam of the WM_KEYUP message to obtain the bits I'm interested in? – cpoliver Jan 06 '14 at 22:47
  • I don't believe you can, short of tracking the previous state yourself. That's what makes it a *low level* hook. – Igor Tandetnik Jan 06 '14 at 23:40

1 Answers1

0

As Igor kindly pointed out, I had to manage this myself.

The solution was simply a case of having a static list of ints for any key down that has been received without a key up event to follow.

private static readonly List<int> KeysDown = new List<int>();

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode >= 0)
    {
        var keycode = Marshal.ReadInt32(lParam);

        if (wParam == (IntPtr)WM_KEYUP)
        {
            KeysDown.RemoveAll(k => k == keycode);
        }

        if (wParam == (IntPtr)WM_KEYDOWN)
        {
            if (!KeysDown.Contains(keycode))
            {
                // Do stuff!
            }

            KeysDown.Add(keycode);
        }

    }

    return CallNextHookEx(_hookId, nCode, wParam, lParam);
}
cpoliver
  • 65
  • 5