Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using SetWindowsHookEx() function for installing a keyboard hook for my remote desktop application in C#. I am using Windows Form Application on Visual Studio 2010, 32 bit Win 8.1. My code to start the hook is

C#
private void Start()
{
    IntPtr handle = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
    Proc = new HookProc(CallBackHook);
    IntPtr HookHandle = SetWindowsHookEx(HookType.WM_KEYBOARD_LL, Proc, handle, 0);
    if (HookHandle == null || HookHandle == IntPtr.Zero)
    {
        Console.Write("Error while installing hook");
    }
}


The code works fine and captures keyboard activities for all processes but how to limit it for current process only. I tried specifying the thread id also but wont works.

C#
HookHandle = SetWindowsHookEx(HookType.WM_KEYBOARD_LL, Proc, handle, Thread.CurrentThread.ManagedThreadId);

Can you please suggest me how to do that. Thanks in advance.
Posted
Updated 19-Jan-14 17:37pm
v2
Comments
BillWoodruff 19-Jan-14 5:59am    
Are you using Global Hook code from here on CP ? If so, please specify the source.

Is this a Windows Forms application ?
[no name] 19-Jan-14 23:14pm    
@BillWoodruff Yes i am using window Form application.

1 solution

The last parameter should be: AppDomain.GetCurrentThreadId()
 
Share this answer
 
Comments
[no name] 19-Jan-14 23:15pm    
I did but doesnt works the VS also shows that AppDomain.GetCurrentThreadId() is a deprecated method. I also tried Thread.CurrenThread.ManagedThreadId but also not worked for me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900