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 get process's name which triggered mouse click #43

Open chungvinhkhang opened 9 years ago

chungvinhkhang commented 9 years ago

Hi, Is there param that contains process (id or name) which triggered the events (click, key,..)?

gmamaladze commented 9 years ago

No but you can get a name of a process with active window somewhere else. e.G.

[DllImport("user32.dll")]
public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

string GetActiveProcessFileName()
{
    IntPtr hwnd = GetForegroundWindow();
    uint pid;
    GetWindowThreadProcessId(hwnd, out pid);
    Process p = Process.GetProcessById((int)pid);
    p.MainModule.FileName.Dump();
}
jonkrusell commented 7 years ago

@chungvinhkhang did you ever find a way to get the id or name of the process that triggered the event?