【问题标题】:SetWindowsHookEx not working properlySetWindowsHookEx 无法正常工作
【发布时间】:2017-10-15 00:24:13
【问题描述】:

我尝试使用SetWindowsHookEx() 创建一个全局鼠标热键,所以当我按下鼠标右键时,它会执行一些代码。我的问题是,当回调函数被执行时,它给了我一个奇怪的数字,代表每次代码执行时都会不断变化的右按钮。

private static IntPtr ButtonHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {

        int button = Marshal.ReadInt32(lParam);
        if (nCode >= 0 && wParam == (IntPtr)WM_RBUTTONDOWN)
        {
            if (button == 0x02)
            {
                _m.rtbLog.AppendText("Test");
            }

        }

        return CallNextHookEx(MainWindow._hookId, nCode, wParam, lParam);
    }

当我阅读 lParam 时,它给了我一个大约 600 的数字,并且每次执行代码时都会发生变化,即使它应该是 0x2。 我该如何解决这个问题:P。

编辑:当我使用按键时它可以完美运行

【问题讨论】:

  • 根据documentation lParam 包含一个指向MSLLHOOKSTRUCT 结构的指针。 This struct 基本上包含鼠标光标的 x 和 y 坐标。为什么应该是0x2
  • 这是什么钩子?键盘钩还是鼠标钩?你是怎么安装的?

标签: c# wpf callback setwindowshookex global-hotkey


【解决方案1】:

0x02 表示WM_MOUSEMOVE,它应该来自wParam 而不是lParamlParam 表示MOUSEHOOKSTRUCT,因为包含x- and y-coordinates of the cursor 会经常变化。请参考[MouseProc callback function][1][MOUSEHOOKSTRUCT structure][2]

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 1970-01-01
    • 2016-09-01
    • 2012-07-11
    • 2018-04-08
    • 2017-04-20
    • 2018-10-02
    • 2016-09-04
    • 2010-10-06
    相关资源
    最近更新 更多