密钥捕获:WM_KEYDOWN最后密钥状态位始终== 0

时间:2014-01-06 20:45:19

标签: c# com interop bitmask

我正在编写的实用程序需要使用SetWindowsHookEx函数来捕获具有LowLevelKeyboardProc回调的按键。

我已经实现了this MSDN blog上的代码。

正在捕获正确的密钥代码,但是当使用从StackOverflow问题的答案中获取的位掩码时,我似乎无法获得第30位的正确值:Get 30th bit of the lParam param in WM_KEYDOWN message

private const int LAST_KEY_STATE_BITMASK = 0x40000000;

根据Microsoft documentation,此位应该允许我检测是否按下了按键。

我只想在第一次按下按键并且没有按下按键时对keydown事件采取行动; keyup是不够的。

我用来尝试获取第30位值的代码是:

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

根据我的理解,当没有按下按键时应该返回0,而当按下按键时应该返回1。

我也尝试使用lParam.ToInt32()(int)lParam代替vkCode,但无济于事。

请帮忙!

这是完整的程序,因为它处于当前状态:

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);
}

1 个答案:

答案 0 :(得分:0)

正如伊戈尔亲切地指出的那样,我必须自己管理。

解决方案只是在没有关键事件发生的情况下收到任何关键字的静态列表。

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);
}