Get Key Pressed if Program is minimized

Heyho Guys!

I am a very new C++ programmer, so I'll problably going to ask a lot of questions the next few weeks.

My first question is this one:
I want to create a console application which should be able to check for some keys even if it is minimized. For instance, the Windows Media Player reacts on pressing the Play/Pause Button if its minimized. How can I do this for my own program?
There are probably a half dozen or so possible ways to do this but to me the most straight forward way would be to use a named pipe. You would write the name of the pipe out to a file or something that your Macro's can reference.

The function "SendMessage()" might seem like it would be easier to use, but then you'd have to grab the window handle every time you did this.
Look at RegisterHotKey() API:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309%28v=vs.85%29.aspx


You have some example code in that page.
It seems that I need some extra libraries for the code on that page on registerHotKey, at least it doesn't know the imported lib in my code. How can I include these Libraries?
On the page is a link to a code collection, but I can't find the corresponding code in the list of possible downloads...
Last edited on
I can't import the stdafx.h library, Eclipse CDT can't find it.
You don't need stafx.h, all headers you need is windows.h and perhaps tchar.h. Don't you read RegisterHotKey requirements ?

I assumed that you are capable already to change that example to your needs.
OK; I changed the Code so that it shows no errors any more. But when I try it, it doesn't show any ouput. I experimented and found out, that nor messages are retrieved by getMessage, so It does nothing but waiting for incoming messages which never come. What can I do to fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (RegisterHotKey( NULL, 1, MOD_SHIFT, 0x42)) {
	    std::cout << "Hotkey 'SHIFT+b' registered, using MOD_NOREPEAT flag\n";
	} else {
	   	std::cout << GetLastError();
	}

//*

	MSG msg = {0};
	while (GetMessage(&msg, NULL, 0, 0) != 0)
	{
		std::cout << "\nMessage:" << msg.message;
		if (msg.message == WM_HOTKEY)
		{
			std::cout << " -> WM_HOTKEY received";
		}
	}


A weir thing is that I don't even see the output of the first cout. Only if I add a std::cin >> i at the position marked with a * I can see the output "Hotkey registered..."
But it also seems that the program does something, because I can't type Shift+B while its running, if I try, nothing happens.
Last edited on
There is something wrong with your setup, as following code works for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <windows.h>


int main()
{
    if (RegisterHotKey( NULL, 1, MOD_SHIFT, 0x42))
    {
        std::cout << "Hotkey 'SHIFT+b' registered, using MOD_NOREPEAT flag\n";
    }
    else
    {
        std::cout << "Error code " << GetLastError();
    }


    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0) != 0)
    {
        std::cout << "\nMessage:" << msg.message;
        if (msg.message == WM_HOTKEY)
        {
            std::cout << " -> WM_HOTKEY received";
        }
    }


    return 0;
}



1st: If I add cout.flush(); the output works.
but 2nd: It only works if the program has the focus, not if it is minimized as I wanted it to.

EDIT: It works if I start the programm directly and not out of the IDE :)

One final Question: How can I get the ID of the hotkey message?
Last edited on
Launch the EXE directly, not from your IDE, no std::flush required. And yes, it works even if it has not focus.
Look at WM_HOTKEY message and at MSG structure. The WPARAM member is message identifier that you probably want:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646279(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644958(v=vs.85).aspx
Thanks, this works now!!! :)

...Just one problem: The code blocks any further processing of the event, so I can't type 'B' anymore. How can I stop this?
Ideally this application is not a console one (console works just for testing purpose), but a GUI one (WinMain entry point). A GUI application tipically have a message loop which can be leaved by using PostQuitMessage function.

There are numerous examples out there.
Topic archived. No new replies allowed.