gmamaladze / globalmousekeyhook

This library allows you to tap keyboard and mouse, detect and record their activity even when an application is inactive and runs in background.
MIT License
1.06k stars 256 forks source link

How to hook for a single process? #106

Open ghost opened 5 years ago

ghost commented 5 years ago

I was working on my multiboxing project and I have encountered a problem which is that the mouse hook is causing my cursor to lag. Therefore, I am looking for hooks that don't lag my cursor. Soon, I found this hook and tried to make it work, But, I don't see any way in it to hook for only a process. private IntPtr SetHook(MouseHookHandler proc) { return SetWindowsHookEx(14, proc, GetModuleHandle(Process.GetProcessById(ProcessId).MainModule.ModuleName), GetWindowThreadProcessId(GetModuleHandle(Process.GetProcessById(ProcessId).MainModule.ModuleName), out int gay)); } This is what I have written for my previous mouse hook. If there isn't any method to hook for a process, add it then. If there's, tell me then. Thanks in advance!

thavious commented 5 years ago

I had to take his code and rewrite some of it to handle multiboxing (i.e. he doesn't pass the flags field in his KeyEventArgsExt, so you can't check if it's an injected key and you want to bypass it). I would overload his HookApp function and pass the threadid of the process you are trying to hook (in his code he just calls GetCurrentThreadId)

The other would be to use the global hook and get the id from the current foreground window (this can be messy due to timing if you are bringing windows forward to inject keys at them).

I personally use a global hook because I don't care which process the key is pressed for, I just care where the key is going, but I see how less processing can make things run smoother.

smallprogrammers commented 4 years ago

@RageXOfficial hello did you success on that ? I need exact the same thing. Pls guide me

ghost commented 4 years ago

@RageXOfficial hello did you success on that ? I need exact the same thing. Pls guide me

Well, replying to your question, Idk how I succeeded on that, but I think it is necessary to PM me, because you may want to see how I implemented it in my multiboxer. Other than calling and importing Win32 API, you could still write a C++ dll and inject it into a process you want to hook. To accomplish it, you have to write a bunch of code that run local keys hook, handling pressed key from a specific process for the dll.