Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. KeyBoard hook Doubt

KeyBoard hook Doubt

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _anil_
    wrote on last edited by
    #1

    Hello I am doing some keyboard hook program. I am able to hook it and disable the inputs of some keys in the application. But If I am shifting to other application say VC++ id and coming back to my application the application no more hooking the keys. I want to hook the keys for all the application. i.e I want to disable some keys for all the application. Is it possible? give me some hint to proceed Thanks

    Regards Anil

    N 1 Reply Last reply
    0
    • _ _anil_

      Hello I am doing some keyboard hook program. I am able to hook it and disable the inputs of some keys in the application. But If I am shifting to other application say VC++ id and coming back to my application the application no more hooking the keys. I want to hook the keys for all the application. i.e I want to disable some keys for all the application. Is it possible? give me some hint to proceed Thanks

      Regards Anil

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      For a hook to be globel, u must write the hook procedure in a dll. Paste the code here so that we can check whats wrong with it..

      nave

      _ 1 Reply Last reply
      0
      • N Naveen

        For a hook to be globel, u must write the hook procedure in a dll. Paste the code here so that we can check whats wrong with it..

        nave

        _ Offline
        _ Offline
        _anil_
        wrote on last edited by
        #3

        Thanks for your quick response I have not done much but still you can check the code This is the code from the dll Just have 3 function now Install Uninstall and the global Keyboar procedure. LRESULT CKeyHook::InstallKeyHook() { g_hHook = ::SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, AfxGetInstanceHandle(), 0); return( KH_OK ); } LRESULT CKeyHook::UninstallKeyHook() { ::UnhookWindowsHookEx(g_hHook); return( KH_OK ); } LRESULT CALLBACK KeyboardProc(INT nCode, WPARAM wParam, LPARAM lParam) { switch (nCode) { case HC_ACTION: { // Disable All return 1; } } return ::CallNextHookEx(g_hHook, nCode, wParam, lParam); } The test application just call InstallKeyHookin InitDialog and UnInstallKeyHook in OnClose.

        Regards Anil

        N 1 Reply Last reply
        0
        • _ _anil_

          Thanks for your quick response I have not done much but still you can check the code This is the code from the dll Just have 3 function now Install Uninstall and the global Keyboar procedure. LRESULT CKeyHook::InstallKeyHook() { g_hHook = ::SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, AfxGetInstanceHandle(), 0); return( KH_OK ); } LRESULT CKeyHook::UninstallKeyHook() { ::UnhookWindowsHookEx(g_hHook); return( KH_OK ); } LRESULT CALLBACK KeyboardProc(INT nCode, WPARAM wParam, LPARAM lParam) { switch (nCode) { case HC_ACTION: { // Disable All return 1; } } return ::CallNextHookEx(g_hHook, nCode, wParam, lParam); } The test application just call InstallKeyHookin InitDialog and UnInstallKeyHook in OnClose.

          Regards Anil

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          _anil_ wrote:

          g_hHook = ::SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, AfxGetInstanceHandle(), 0);

          i think the problem is with the AfxGetInstanceHandle() used here. What i suggest is create a globel variable of HINSTANCE and set this parameter with the HINSTACE coming in the dll main. If ur using an MFC dll, set this variable as follows BOOL CDllApp::InitInstance() { AFX_MANAGE_STATE(AfxGetStaticModuleState()); hins=AfxGetInstanceHandle(); return CWinApp::InitInstance(); } now pass this variable in the SetWindowsHookEx function

          nave

          _ 1 Reply Last reply
          0
          • N Naveen

            _anil_ wrote:

            g_hHook = ::SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, AfxGetInstanceHandle(), 0);

            i think the problem is with the AfxGetInstanceHandle() used here. What i suggest is create a globel variable of HINSTANCE and set this parameter with the HINSTACE coming in the dll main. If ur using an MFC dll, set this variable as follows BOOL CDllApp::InitInstance() { AFX_MANAGE_STATE(AfxGetStaticModuleState()); hins=AfxGetInstanceHandle(); return CWinApp::InitInstance(); } now pass this variable in the SetWindowsHookEx function

            nave

            _ Offline
            _ Offline
            _anil_
            wrote on last edited by
            #5

            Superb Its worked.:-D Now please tell me what is the difference beteewn the two?:doh: Why did you think that that should be the error place.

            Regards Anil

            N 1 Reply Last reply
            0
            • _ _anil_

              Superb Its worked.:-D Now please tell me what is the difference beteewn the two?:doh: Why did you think that that should be the error place.

              Regards Anil

              N Offline
              N Offline
              Naveen
              wrote on last edited by
              #6

              sorry it wasn't the problem of taking the hinstance value in InitInstance. It was the problem of not calling AFX_MANAGE_STATE(AfxGetStaticModuleState()); i tried calling AfxGetInstanceHandle() from a dll as follow. HINSTANCE h2 = AfxGetInstanceHandle();// Returns the instance of the exe AFX_MANAGE_STATE(AfxGetStaticModuleState()); HINSTANCE h = AfxGetInstanceHandle();// Returns the instance of the dll now i think u understood the problem.

              nave

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups