I am using the global keyboard and mouse hook for tacking the keyboard and mouse activity. I am facing the issue like when user uses team viewer or Remote desktop (connects to remote machine) then we are not able to get the keyboard and mouse hook (the hook is on the local machine). We have a background timer running which keep on checking the when was the lastinput time then if it is greater than 1 min i will uninstall the hook and install it. when we do this i am getting the unique pointer(ptrHook) every time we uninstall and install the hook but i am not able to listen to events. once the hook is lost even though after uninstalling and installing the hook not able to get the events.
For mouse Hook
public void InstallHook(int processId)
{
try
{
ProcessModule objCurrentModule = null;
objCurrentModule = Process.GetProcessById(processId).MainModule;
objMouseProcess = new LowLevelMouseProc(captureMouse);
//In order to avoid memory access violation error allocate the memory from GCHandle
//Refer :http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/b0defb8f-1242-4485-a680-8773da07db2f
GCHandle.Alloc(objMouseProcess);
ptrHook = SetWindowsHookEx(WH_MOUSE_LL, objMouseProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
}
}
For Keyboard Hook
public void InstallHook(int processId)
{
try
{
ProcessModule objCurrentModule = null;
objCurrentModule = Process.GetProcessById(processId).MainModule;
objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
//In order to avoid memory access violation error allocate the memory from GCHandle
//Refer :http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/b0defb8f-1242-4485-a680-8773da07db2f
GCHandle.Alloc(objKeyboardProcess);
ptrHook = SetWindowsHookEx(WH_KEYBOARD_LL, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
}
}
CallNextHookEx
then you will have an issue – Shipmate