Hi all,
i'm trying to monitor the system for hotkeys like powerstrip it monitors for hotkeys to increase or decrease gamma or whatever while any thing is active the first thing i thought about was hooks so i wrote my hook procedure in a dll setup the hook like this
///code in exe
gkbhookdll = LoadLibrary("kbhook.dll");//load dll
gkeyboardhook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)GetProcAddress(gkbhookdll,"KeyProc"),gkbhookdll,0);
i specified 0 for last parameter to be system wide every thing was ok
now strange problem is that when i activate another window then the hook doesn't work even if i reactivated my window, any one knows what's happening.
does CallNextHookEx returns something that Unhooks my hook?
Thanks for advance and if any one knows a better way for monitoring system for specific hotkeys please tell me, thanks every body again.
and if some body knows how to format parameters returned from the hook with CHotKeyCtrl.GetHotKey is welcome because of the parameter holding wModifiers.
The hook can only give you the code of one button and tell you if alt is pressed. Thanks for the third time.
Last edited by snakekain; March 4th, 2005 at 07:43 AM.
now strange problem is that when i activate another window then the hook doesn't work even if i reactivated my window, any one knows what's happening.
does CallNextHookEx returns something that Unhooks my hook?
Thanks for advance and if any one knows a better way for monitoring system for specific hotkeys please tell me, thanks every body again.
and if some body knows how to format parameters returned from the hook with CHotKeyCtrl.GetHotKey is welcome because of the parameter holding wModifiers.
The hook can only give you the code of one button and tell you if alt is pressed. Thanks for the third time.
Calling CallNextHookEx is optional, but it is highly recommended; otherwise, other applications that have installed hooks will not receive hook notifications and may behave incorrectly as a result. You should call CallNextHookEx unless you absolutely need to prevent the notification from being seen by other applications.
Well maybe there is a program that doesn't want you to receive hook's.
Maybe you need to replace this:
LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
with this:
__declspec(dllexport) LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
But this would be if its not working at all. But you say that the hook works until you switch to another app? You do see the 'called hook' message box at first? If thats so then the __declspec probably isnt the solution.
Maybe you need to replace this:
LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
with this:
__declspec(dllexport) LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
But this would be if its not working at all. But you say that the hook works until you switch to another app? You do see the 'called hook' message box at first? If thats so then the __declspec probably isnt the solution.
No, if he uses a definition file (.def extension) he doesn't need the __declspec() export method:
Code:
LIBRARY MyHookDll
EXPORTS
KeyProc @1
This will do the same. And I recommend this method if you use LoadLibrary() and GetProcAddress().
Sorry I was just comparing with my hook code & that was the only difference I saw but now that I think about it that shouldnt matter (your not exporting the hook procedure so you dont need it).
Anyway heres my KeyboardHook dll project. It's pretty simple, use
StarHook(GetCurrentProcessId(), hWindowHandle);
// Tell the hook procedure to send keyboard messages from different processes to this process' hWindowHandle
then call SetHook(true);
and call EndHook() at the end of the program.
Last edited by Martin O; March 4th, 2005 at 10:13 AM.
Thank you guys for all those replys, of course i exported the function using the def file, when i read the tutorial from http://www.codeguru.com/Cpp/W-P/syst...cle.php/c5699/ it setup the hook from the dll but i did it from the exe. I looked at RegisterHotKey (thanks to god then to NoHero) which is a more convenient way it worked very well but a small problem exist however that if i set the hot key to Q for example switching to another task pressing Q doesn't work for the current window as if i'm not pressing it but the message box appears at my window that means all apps will not be aple to work properly any help will be appreciated, thanks again for advance.
Thanks nohero here's the problem i use
RegisterHotKey(m_hWnd,1,0,(int)'Q'); to setup the hot key at the start up of the program when i start the program the wm_hotkey message key is sent with the right parameters, if i leave my program open and activate notepad to write something in it if i pressed the hot key 'Q' in this example then nothing happen the notedpad doesn't write any thing may be that means it didn't recieve wm_keydown message that happens for every other window, thanks guys again.
Last edited by snakekain; March 6th, 2005 at 11:27 AM.
Thanks nohero here's the problem i use
RegisterHotKey(m_hWnd,1,0,(int)'Q'); to setup the hot key at the start up of the program when i start the program the wm_hotkey message key is sent with the right parameters, if i leave my program open and activate notepad to write something in it if i pressed the hot key 'Q' in this example then nothing happen the notedpad doesn't write any thing may be that means it didn't recieve wm_keydown message that happens for every other window, thanks guys again.
Of course. This is normal behaviour of RegisterHotkey. Just read MSDN:
The RegisterHotKey function defines a system-wide hot key.
If you want the hotkey to only work in your application then you should use Accelerators. This works as followed:
Create a new accelerator resource in the resource editor.
Assign a hotkey to an ID of a button (for example)
MSDN msg = {0};
int Ret = 0;
HACCEL hAccel = NULL;
// Load resource
hAccel = LoadAccelerators(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_MYACCELTABLE));
// Assuming that hwnd is the handle to your window
while ( (bRet = GetMessage(&msg, hwnd, 0, 0)) != 0 )
{
if ( bRet == -1 )
{
// error
}
else
{
if ( !TranslateAccelerator(hwnd, hAccel, &msg) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
If you have any further problems with it, you should post your entire project, so I can help you out directly at the kernel of the problem. If you have any further questions on this, feel free to ask.
Thank you for your reply, sorry for taking a lot of your time, i need a system-wide hotkey but i don't want it to prevent every other application from recieving the key because that will annoy the user, i want my app in the system tray recieving the hot key messages, while the user works with other apps uninterrupted.
In the example i gave the user will be bothered a lot finding that pressing Q will not write Q in the note pad or any other app while my app is working.
If that'll not work i guess working with system-wide hooks will do because it doesn't prevent other apps from recieving the messages except if i returned 1, and that means i'll have to find a work around to work with the hotkey format returned from CHotKeyCtrl. Thanks again alot, you really helped me out.
Your hook works. Providing you start your hook using StartHook.
You are blocking all keystrokes for all windows but one: the one with handle you send when calling StartHook.
If you want to monitor keyboard it is easier by using WH_KEYBOARD_LL. This is always global hook even if procedure is in an exe module.
AS for CHotKeyCtrl you can pass its window handle and use it to translate keyboard input into a code and key value.
There are only 10 types of people in the world: Those who understand binary and those who do not.
To create a global hotkey that does not interfere with other applications, you can use DirectInput (it is not so complicated to use, and very powerful).
Or you can call at short-intervals, GetAsyncKeyState (under Windows 98, GetAsyncKeyState retrives asynchronously system-wide key strokes, but i don't know if it works under all windows because GetKeyState does not retrives system-wide key strokes).
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.