Mouse hook with C#

后端 未结 4 1966
旧巷少年郎
旧巷少年郎 2021-01-15 01:08

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 o

相关标签:
4条回答
  • 2021-01-15 01:54

    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.

    0 讨论(0)
  • 2021-01-15 01:57

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

    0 讨论(0)
  • 2021-01-15 01:58

    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.

    0 讨论(0)
  • 2021-01-15 02:04

    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.

    0 讨论(0)
提交回复
热议问题