0

I've got a specific application which I can find using

Process.GetProcesses()

and filtering by ProcessName. I'd like to filter out all keypress-events of that process, unfortunately one can only pass a optional thread-id to SetWindowsHookEx as last parameter.

That's why I thought about filtering the incoming events but I can't find a way to retrieve the information where it came from. Is there any solution to do it?

The callback-information are provided within LowLevelKeyboardProc having another struct inside lparam: KBDLLHOOKSTRUCT

isHuman
  • 125
  • 9
  • 1
    The keystroke goes to the window in the foreground. So you need GetForegroundWindow() and GetWindowThreadProcessId(). – Hans Passant Nov 23 '15 at 15:02
  • @Hans Passant: This was helpful. Please post an answer so I can mark this as solved. `Process process = Process.GetProcesses() .Where(x => x.ProcessName == "MyProcessName") .FirstOrDefault(); //... IntPtr handle = GetForegroundWindow(); uint processID = GetWindowThreadProcessId(handle, IntPtr.Zero); if (p2.Threads.OfType().Any(x => x.Id == Convert.ToInt32(processID))) //success` – isHuman Nov 30 '15 at 10:07
  • Just post the solution yourself and mark it as the answer. – Hans Passant Nov 30 '15 at 10:13

1 Answers1

0
public partial class Form1 : Form 
{
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

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

    Process process;

    public Form1() 
    {
        process = Process.GetProcesses()
            .Where(x => x.ProcessName == "MyProcessName")
            .FirstOrDefault();

        //init global keypress as needed
    }

    void gkh_KeyUp(object sender, KeyEventArgs e)
    {
        IntPtr handle = GetForegroundWindow();
        uint processID = GetWindowThreadProcessId(handle, IntPtr.Zero);
        if (p2.Threads.OfType<ProcessThread>().Any(x => x.Id == Convert.ToInt32(processID)))
        {
            //keypress in MyProcessName
        }
        e.Handled = true;
    }
isHuman
  • 125
  • 9