Can someone help me with this? I No matter what I do, when I press F9, the message box pops up showing F9, But when I press F10, the message box for F9 pops up.. Is it because MSG msg and MSG msg2 are the same?? How do I fix this, Im not very good at it :S

Basically I want to tell which Global hotkey was pressed.

if (RegisterHotKey(NULL,1,0x4000,0x78))  //F9 Key
		{
			MessageBox::Show("SmartChat -- Registered HotKey: F9");
		}
	 if (RegisterHotKey(NULL,2,0x4000,0x79))  //F10 Key
		{
			MessageBox::Show("Exit -- Registered Hotkey: F10");
		}
				MSG msg = {0};
				MSG msg2 = {0};
				while (GetMessage(&msg, NULL, 0, 0) != 0)
				{
					if(msg.message == WM_HOTKEY)
					{
						MessageBox::Show("F9");
					}
					break;
				}
				while (GetMessage(&msg2, NULL, 0, 0) != 0)
				{
					if(msg.message == WM_HOTKEY)
					{
						MessageBox::Show("F10");
					}
					break;
				}

P.S. I had it working when I put them hotkeys and messages in two separate threads.. that way one thread monitors one key, the next monitors another key.. but it lags me a lot more than when I only had 1 thread. Current program has 4 threads (BackgroundWorkers) and it lags me so bad! So Im trying to reduce it by putting 99% of the code in one thread.

Recommended Answers

All 3 Replies

The second argument to RegisterHotKey() is the id of the hot key. If multiple WM_HOTKEY messages are received by the same thread, you need to examine the lParam of the message for the id of the hot key that was pressed.

enum { F9_KEYID = 1, F10_KEYID = 2 } ;
RegisterHotKey( 0, F9_KEYID, MOD_NOREPEAT, VK_F9 ) ;
RegisterHotKey( 0, F10_KEYID, MOD_NOREPEAT, VK_F10 ) ;

MSG msg ;
while( GetMessage( &msg, 0, 0, 0) )
{
    if(msg.message == WM_HOTKEY)
    {
        switch( HIWORD(msg.lParam) )
        {
            case F9_KEYID : MessageBox::Show("F9 was pressed") ; break ;
            case F10_KEYID : MessageBox::Show("F10 was pressed") ; break ;
            default: MessageBox::Show("some other hot key was pressed") ;
        }
    }
    // ...
}

Tip: avoid using magic numbers in your code.

commented: U ARE AMAZING! +2

The second argument to RegisterHotKey() is the id of the hot key. If multiple WM_HOTKEY messages are received by the same thread, you need to examine the lParam of the message for the id of the hot key that was pressed.

enum { F9_KEYID = 1, F10_KEYID = 2 } ;
RegisterHotKey( 0, F9_KEYID, MOD_NOREPEAT, VK_F9 ) ;
RegisterHotKey( 0, F10_KEYID, MOD_NOREPEAT, VK_F10 ) ;

MSG msg ;
while( GetMessage( &msg, 0, 0, 0) )
{
    if(msg.message == WM_HOTKEY)
    {
        switch( HIWORD(msg.lParam) )
        {
            case F9_KEYID : MessageBox::Show("F9 was pressed") ; break ;
            case F10_KEYID : MessageBox::Show("F10 was pressed") ; break ;
            default: MessageBox::Show("some other hot key was pressed") ;
        }
    }
    // ...
}

Tip: avoid using magic numbers in your code.

Hmm what do u mean avoid using magic numbers?? do u mean the 0x78 and 0x79??

And I just tried what u posted above, that doesnt work.. it always gives me the default "some other hot key was pressed".

Hey thanks.. I just figured it out -__- But u kinda helped me alot.. Iunno why it didnt work earlier when I changed lParam to wParam but it works now with peek message whereas it doesn't work with GetMessage. Anyway THANK YOU!! I will rep u. Especially as you are the only one who helped me :)

Btw it was done like this:

PeekMessage(&msg, 0, 0, 0, 0x0001);
				TranslateMessage(&msg);
				switch(msg.message)
				{
					case WM_HOTKEY:
						if(msg.wParam == F9_KEYID)
						{
							MessageBox::Show("F9 Pressed");
						}
						else if(msg.wParam == F10_KEYID)
						{
							MessageBox::Show("F10 Pressed");
						}
				}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.