11

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);
    }
}
jan_kiran
  • 301
  • 3
  • 16
Ravi Kanth
  • 1,182
  • 13
  • 38
  • I think you will have the same problem if the user is utilizing the on-screen keyboard (osk.exe) or even the ink/tablet inputs. I believe these work by publishing windows messages instead of emulating the low-level keyboard/mouse. – Ron Beyer Apr 03 '18 at 14:50
  • Wat do you mean by publishing windows message can you give any example so that I can tryout.. – Ravi Kanth Apr 03 '18 at 16:06
  • 3
    This is just the universal *what if two programs do this* problem. Remote Desktop and Team Viewer also want to hook the mouse and keyboard since they need to transmit mouse and keyboard events to the remote machine. They got started later, so are always ahead in the hook chain and you inevitably lose. Hacking it isn't that likely to work well either, these programs also have a security problem to solve. They can't allow a hook to spy on remote logins. Feature, not a bug. – Hans Passant Apr 10 '18 at 09:55
  • Please look at the below thread https://stackoverflow.com/questions/26563402/how-to-ensure-setwindowshookex-priority. Also when teamview is connect and it hooks and doesn't call [`CallNextHookEx`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644974(v=vs.85).aspx) then you will have an issue – Tarun Lalwani Apr 10 '18 at 10:09
  • Not a fix but possible workaround describe [here](https://stackoverflow.com/questions/14596117/setwindowshookex-wm-keyboard-ll-not-coming-through-with-full-screen-rdc?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) ? – dsdel Apr 16 '18 at 08:32
  • Maybe this is the issue? https://stackoverflow.com/a/11180812/4146066 – Hans Feb 09 '19 at 17:30

0 Answers0