KeyBoard hook Doubt
-
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
-
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
-
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
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
-
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
_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
-
_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
-
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
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