Unable to get the global Keyboard and Mouse Hook events
Asked Answered
C

0

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);
    }
}
Converter answered 3/4, 2018 at 14:35 Comment(6)
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.Lunnete
Wat do you mean by publishing windows message can you give any example so that I can tryout..Converter
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.Selemas
Please look at the below thread #26563902. Also when teamview is connect and it hooks and doesn't call CallNextHookEx then you will have an issueShipmate
Not a fix but possible workaround describe here ?Gomez
Maybe this is the issue? https://mcmap.net/q/672340/-setwindowshookex-for-wh_mouseDependence

© 2022 - 2024 — McMap. All rights reserved.