Archived

This topic is now archived and is closed to further replies.

System Hotkeys?

This topic is 5654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to register a system hotkey that can be used w/o having a window? Basically I want a program to be running in the background that will display a dialog box whenever a hotkey is pressed. Here is the code I came up with, though it does not work. I''m not sure exactly how RegisterHotkey works, whether it does application hotkeys or system hotkeys, but I tried anyways.
  
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	MSG msg;
	RegisterHotKey(NULL, 1, MOD_CONTROL, VK_RETURN);

	while(true)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);

			if(msg.message = WM_HOTKEY)
				if (int(msg.wParam) == 1)
					CreateDialog(hInstance, MAKEINTRESOURCE(IDD_FILEFINDER), NULL, DialogProc);
			else
				DispatchMessage(&msg);
		}
	}

	UnregisterHotKey(NULL, 1);

	return 0;
}
  
Any ideas on how to make this small program work or am I going about it the whole wrong way?

Share this post


Link to post
Share on other sites
Why bother with hooks? They are complicated because they work in other programs'' address space, besides, they are more of a workaround considering that straightforward RegisterHotKey is present. Anyway.

Ronin Magus: the bugs in your code have to do with C++, not with Win32. First, the innermost if statement must be wrapped with curly braces. As you have it, else branch will be executed only if message is WM_HOTKEY.

Secondly, you use assignment (=) instead of comparison (==).

Lastly, you should use GetMessage and save CPU cycles. If you are only waiting for the hotkey to be pressed, your program uses 100% CPU, which is WAY too much. If this is a game loop, then it''s a whole different matter, of course.

Share this post


Link to post
Share on other sites
Well I didn''t see any problem with my syntax but I followed your advice anyways, and still nothing happens. Has anyone done this before that can give me some pointers? All I want is a dialog box to pop up when a person presses Cntrl+Enter. I already know about hooks, and if a hook is necessary, fine, but I''d like to avoid having to program a hook DLL.


  
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;
RegisterHotKey(NULL, 1, MOD_CONTROL, VK_RETURN);

while(GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg);

if(msg.message = WM_HOTKEY)
{
if (int(msg.wParam) == 1)
{
CreateDialog(hInstance, MAKEINTRESOURCE(IDD_FILEFINDER), NULL, DialogProc);
}
}
else
{
DispatchMessage(&msg);
}
}

UnregisterHotKey(NULL, 1);

return 0;
}



http://www.icarusindie.com/roninmagus

Share this post


Link to post
Share on other sites
The problem with your syntax is on this line:


  
if(msg.message = WM_HOTKEY)


It should look like this:


  
if(msg.message == WM_HOTKEY)


In C++, ''='' is an assignment operator (i.e. a=5). ''=='' is a comparison operator (i.e. if a==5). As it stands, your code will automatically set msg.message to WM_HOTKEY, and will never execute DispatchMessage(&msg).

-Mike

Share this post


Link to post
Share on other sites
For future reference, this code works correctly.

  
RegisterHotKey(NULL, 1, MOD_CONTROL, VK_RETURN);

while(GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg);

if(msg.message == WM_HOTKEY)
{
MessageBox(NULL, "Hotkey", "Hotkey", MB_OK);
DialogBox(hInstance, MAKEINTRESOURCE(IDD_FILEFINDER), NULL, DialogProc);
}

DispatchMessage(&msg);
}


http://www.icarusindie.com/roninmagus

Share this post


Link to post
Share on other sites
I don''t know if there''s a function to retrieve a free hotkey number, but you''ll probably want to use a number that''s more likely to be unique than 1, for compatability with other programs that might be running.

Share this post


Link to post
Share on other sites
quote:
Original post by RQuinn
I don''t know if there''s a function to retrieve a free hotkey number


GlobalAddAtom.
quote:

but you''ll probably want to use a number that''s more likely to be unique than 1, for compatability with other programs that might be running.

Only if your code is in a DLL. It''s all in the docs, read them.

Share this post


Link to post
Share on other sites