如何使用全局键盘钩子和postmessage()模拟按键组合(例如用于选择文本的shift+left)?

发布于 2024-10-30 14:32:11 字数 1320 浏览 0 评论 0原文

我正在使用全局键盘挂钩(WH_KEYBOARD_LL)并将按键发送到浏览器句柄。我能够获取用户按下的单个键,但无法获取按下的按键组合(例如用于选择文本的shift+left)。代码如下...

private IntPtr ProcessKey(int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode >= 0
        && wParam == (IntPtr)WM_KEY_EVENT.WM_KEYDOWN
        || wParam == (IntPtr)WM_KEY_EVENT.WM_SYSKEYDOWN)
    {
        int vkCode = Marshal.ReadInt32(lParam);
        int vkCode1 = Marshal.ReadInt32(wParam);//here I am getting runtime
        //error as Attempted to read or write protected memory.
        //This is often an indication that other memory is corrupt. 

        SafeNativeMethods.PostMessage(m_browserHandle,(uint)WM_KEY_EVENT.WM_KEYDOWN,  
            Convert.ToInt32((System.Windows.Forms.Keys)vkCode),
            Convert.ToInt32((System.Windows.Forms.Keys)vkCode1));
    }

    return SafeNativeMethods.CallNextHookEx(_hookID, nCode, wParam, lParam);
}

[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

public static class WM_KEY_EVENT
{
    public static int WM_KEYDOWN = 0x0100;
    public static int WM_SYSKEYDOWN = 0x0104;
    public static int WM_KEYUP=0x0101;
    public static int WM_SYSKEYUP=0x0105;
};

我读了一些可以获取组合键的地方使用 wParam 来控制按键,这会产生如上面代码所示的错误。请建议如何避免该错误或其他替代方法。

I am using a global keyboard hook (WH_KEYBOARD_LL) and sending the keys to a browser handle. I am able to get a single key pressed by the user but not able to get the combination of keys pressed(such as shift+left for selecting text).The code goes below...

private IntPtr ProcessKey(int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode >= 0
        && wParam == (IntPtr)WM_KEY_EVENT.WM_KEYDOWN
        || wParam == (IntPtr)WM_KEY_EVENT.WM_SYSKEYDOWN)
    {
        int vkCode = Marshal.ReadInt32(lParam);
        int vkCode1 = Marshal.ReadInt32(wParam);//here I am getting runtime
        //error as Attempted to read or write protected memory.
        //This is often an indication that other memory is corrupt. 

        SafeNativeMethods.PostMessage(m_browserHandle,(uint)WM_KEY_EVENT.WM_KEYDOWN,  
            Convert.ToInt32((System.Windows.Forms.Keys)vkCode),
            Convert.ToInt32((System.Windows.Forms.Keys)vkCode1));
    }

    return SafeNativeMethods.CallNextHookEx(_hookID, nCode, wParam, lParam);
}

[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

public static class WM_KEY_EVENT
{
    public static int WM_KEYDOWN = 0x0100;
    public static int WM_SYSKEYDOWN = 0x0104;
    public static int WM_KEYUP=0x0101;
    public static int WM_SYSKEYUP=0x0105;
};

I read some where that we can get the combination of key press by using wParam, which gives error as shown in the code above. Please suggest how to avoid that error or some alternative way to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2024-11-06 14:32:11

您的代码有一些错误。您将 wParam 视为指针(因为您用它调用 ReadInt32 ),但根据 文档 它包含窗口消息。

lParam 您应该取消引用(使用 Marshal.PtrToStructure)到 KBDLLHOOKSTRUCT,它包含按键代码和修饰键状态。

我不认为将 vkCode 转换为 System.Windows.Fórms.Keys 值,然后再次转换回 int 有何意义。

Your code has some errors in it. You're treating wParam as a pointer (since you're calling ReadInt32 with it), but according to the documentation it contains the window message.

lParam you should derefernece (using Marshal.PtrToStructure) to a KBDLLHOOKSTRUCT, it contains the key code and modifier key state.

And I don't see the point in casting vkCode to a System.Windows.Fórms.Keys value, and then right back to an int again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文