C# 的鼠标钩子

发布于 2024-10-02 04:54:25 字数 345 浏览 0 评论 0原文

我正在尝试模拟“硬件”鼠标点击,因为某些软件似乎会阻止来自 PostMessage 的输入。我知道有 SendInput,但这不是一个选项,因为我也需要它在后台窗口中兼容。解决方案似乎是一个低级鼠标钩子,但我四处搜索,除了记录器之外找不到任何东西,没有移动鼠标、单击等操作。我希望这种情况发生而无需编写一些代码某种 C++/C 包装器,用作假鼠标驱动程序。

http://support.microsoft.com/kb/318804,我找到了这个,但它没有似乎没有任何进一步的帮助。

任何帮助表示赞赏:)

I am trying to emulate "hardware" mouse clicks as it appears that some software blocks input from PostMessage for instance. I know there's SendInput, but that's not an option as I need this to be compatible in background windows as well. The solution seems to be a low-level mouse hook but I've searched around and couldn't find anything other than just the loggers, no manipulation of moving mouse, clicking etc. I'd like this to happen without having to write some sort of C++/C wrapper to use as a fake mouse driver.

http://support.microsoft.com/kb/318804, I found this but it doesn't seem to be of any further help.

Any help appreciated :)

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

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

发布评论

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

评论(4

七分※倦醒 2024-10-09 04:54:25

不确定“某些软件”可能是什么,但可以肯定的是,UAC 会阻止您将消息插入提升的程序的窗口中。它称为 UIPI,即用户界面权限隔离。

一般来说,用 PostMessage 伪造输入根本不起作用。这对于键盘输入来说尤其是一个问题,但是鼠标输入也有麻烦。没有好的方法可以改变另一个进程的键盘状态。当程序在处理输入消息时检查 Shift、Ctrl 和 Alt 键的状态时,这一点很重要。很多人都这样做。

唯一真正的解决方案是使用 SendInput() 模拟输入。现在你有一个焦点问题需要解决。

Not sure what 'some software' might be, but sure, UAC stops you from poking messages into the windows of elevated programs. It is called UIPI, User Interface Privilege Isolation.

In general, faking input with PostMessage doesn't work well at all. It is especially a problem for keyboard input but mouse input has trouble too. There is no good way to alter the keyboard state for another process. That matters when the program checks the state of the Shift, Ctrl and Alt keys when it processes the input message. Many do.

The only real solution is to emulate input with SendInput(). Now you got a focus problem to solve.

匿名的好友 2024-10-09 04:54:25

mouse_eventSendInput 用于注入鼠标输入。但就像真正的鼠标一样,它是全局输入,不能在隐藏窗口上工作。

低级鼠标钩子也是全局的,但它用于拦截和操作鼠标输入,而不是注入输入。

当定位特定窗口时,您需要使用 SendMessage,但正如您所指出的,它并不适用于所有情况。

您还可以使用 dll 挂钩(例如 IAT 挂钩)来拦截对返回全局光标位置或鼠标按钮状态的 API 的调用。但为此,您需要将 dll 注入目标应用程序,并且该 dll 不应使用 .net。

mouse_event or SendInput used to inject mouse input. But just like a real mouse it's global input and can't work on hidden windows.

A low-level-mouse-hook is global too, but it is used to intercept and manipulate mouse-input, not to inject input.

When targeting a specific window you'll need to use SendMessage, but as you noted it doesn't work for everything.

You can also use dll hooking(for example an IAT hook) to intercept calls to APIs which return the gobal cursor position or the state of the mousebuttons. But for that you need to inject a dll into the target application, and that dll shouldn't use .net.

一杆小烟枪 2024-10-09 04:54:25

当我必须模拟鼠标输入时,我首先尝试使用 SendMessage,但有时某些控件或应用程序可能会吃掉该消息。
在这种情况下,我使用间谍++来拦截持有控件的窗口的消息,我完全按照我想要模拟的方式进行操作,然后,我只需使用:

GetWindowLong(hwnd, GWL_WNDPROC);

来获取窗口过程然后直接使用

以下命令调用 wnd proc(process): CallWindowProc(WNDPROC lpPrevWndFunc, HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)

发送我使用 Spy++ 看到的消息。这总是有效的,因为窗口过程是立即调用的,而不是在消息循环中排队。

When I have to simulate mouse input I first try with SendMessage but sometimes some control or the application could eat the message.
In that situations I use spy++ to intercept messages of the window that holds the control, I do exactly what I want to simulate and then, I just use:

GetWindowLong(hwnd, GWL_WNDPROC);

to get window proc and then call the wnd proc(process) directly with:

CallWindowProc(WNDPROC lpPrevWndFunc, HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)

Sending exactly those messages that I saw using Spy++. That always work because the window proc is called immediately instead of queued in the message loop.

箜明 2024-10-09 04:54:25

看一下这个库 http://globalmousekeyhook.codeplex.com/
它是 100% 托管的 C# 代码,用于安装全局鼠标和键盘挂钩。

Take a look at this library http://globalmousekeyhook.codeplex.com/.
It is 100% managed c# code to install global mouse and keyboard hooks.

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