PeekMessage

I need to do the following:
1. Peek windows message
2. If message is "User pressed ESC key", do nothing, else translate and dispatch the message.

How can i modify the below code to suit my need?


MSG msg;
while(PeekMessage(&msg, NULL, 0, 0,PM_NOREMOVE))
{
   if (msg.message == WM_KEYDOWN &&  msg.wParam == VK_ESCAPE )
   {
        //??????
   }
   GetMessage(&msg,NULL,0,0);
   TranslateMessage(&msg);
   DispatchMessage(&msg);
}
leowlfAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mikeblasConnect With a Mentor Commented:
The problem is that you're using PeekMessage().  Why is that?  You should be using GetMessage().

I guess you're using PeekMessage() because you don't want to block when no message is around.  Then, you should code:


   MSG msg;
   while(PeekMessage(&msg, NULL, 0, 0,PM_NOREMOVE))
   {
      GetMessage(&msg,NULL,0,0);
      if (msg.message != WM_KEYDOWN || msg.wParam != VK_ESCAPE )
      {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
      }
   }

If you're writing an MFC application, it seems like you should be able to find a more appropriate override to realize this functionality.  But it's pretty wierrd functionality: what are you really trying to do? Do you want to leave the WM_KEYDOWN/VK_ESCAPE messages in the queue for some reason?  Why?

..B ekiM


0
 
inprasCommented:
while(PeekMessage(&msg, NULL, 0, 0,PM_NOREMOVE))
{
   if (msg.message == WM_KEYDOWN &&  msg.wParam == VK_ESCAPE )
   {
        continue;
   }
   GetMessage(&msg,NULL,0,0);
   TranslateMessage(&msg);
   DispatchMessage(&msg);
}

0
 
leowlfAuthor Commented:
That won't work.
It will go into a infinite loop if the user press the ESC key, because the ESC message will forever remains in the message queue.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Vinayak KumbarSr Program ManagerCommented:
Hi,

How about manipulating the ESCAPE massage with something else that Ur program does not need?

while(PeekMessage(&msg, NULL, 0, 0,PM_NOREMOVE))
{
if (msg.message == WM_KEYDOWN &&  msg.wParam ==
                               VK_ESCAPE )
   {
         msg.wParam = 0;// nullify that message
    }
GetMessage(&msg,NULL,0,0);
TranslateMessage(&msg);
DispatchMessage(&msg);
}

here instead of making it null, u can modify it to some other kay. And u can modify the msg.message to something else and dispach those messages.

Try it out.

VinExpert
0
 
inprasCommented:
Where is this code of URs U have put?
0
 
inprasCommented:
Where is this code of URs U have put?
I mean in which function?
Regards
0
All Courses

From novice to tech pro — start learning today.