Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are "TranslateMessage" and "DispatchMessage" separate calls?

Tags:

winapi

Most of the Win32 main loops I've seen are all structured like:

while (GetMessage(&message, NULL, 0, 0) > 0) {   TranslateMessage(&message);   DispatchMessage(&message); } 

It was pointed out to me that MsgWaitForMultipleObjects may be used to add some variety to a main loop. But is there a scenario where doing something between GetMessage, TranslateMessage and DispatchMessage is actually useful?

like image 493
Stéphan Kochen Avatar asked Jun 30 '10 17:06

Stéphan Kochen


People also ask

What is the difference between GetMessage () and DispatchMessage () function?

GetMessage pulls the WM_LBUTTONDOWN message from the queue and fills in the MSG structure. Your program calls the TranslateMessage and DispatchMessage functions. Inside DispatchMessage, the operating system calls your window procedure. Your window procedure can either respond to the message or ignore it.

How does TranslateMessage work?

But TranslateMessage is said to translate the virtual keystrokes to characters and post them back to the caller thread's message queue. DispatchMessage directs the OS to call the Windows Procedure of appropriate target window.

What is the difference between GetMessage and PeekMessage?

The main difference between the two functions is that GetMessage does not return until a message matching the filter criteria is placed in the queue, whereas PeekMessage returns immediately regardless of whether a message is in the queue.

What is TranslateMessage?

TranslateMessage() converts virtual keys messages to character input messages. It is a separate call for the remote chance that under certain circumstances you would want to not produce character input messages for certain virtual keys.


2 Answers

The more traditional message loop looks like this:

while (GetMessage(&msg, 0, 0, 0))  {     if (!TranslateAccelerator(hwndMain, haccel, &msg))     {         TranslateMessage(&msg);          DispatchMessage(&msg);      }  } 

It is a pretty big hint to what you'd want to do before dispatching the message: catch messages that ought to be intercepted and treated specially before the window sees them. Keyboard shortcuts are a classic example, they need to be detected no matter what window has the focus.

Any GUI class library exposes it with a virtual method named something like App.PreProcessMessage, a virtual function that can be overridden so your program can implement its own shortcuts and whatnot.

like image 149
Hans Passant Avatar answered Sep 21 '22 11:09

Hans Passant


They are different beasts.

For TranslateMessage function

Translates virtual-key messages into character messages. The character messages are posted to the calling thread's message queue, to be read the next time the thread calls the GetMessage or PeekMessage function. [...] The TranslateMessage function does not modify the message pointed to by the lpMsg parameter.

DispatchMessage, on the other hand, dispatches a message to a window procedure.

So DispatchMessage does the actual work of processing the message. TranslateMessage MAY or MAY NOT post a new message to the thread queue. If the message is translated then a character message is posted to the thread's message queue.

The TranslateMessage function does not modify the message pointed to by the lpMsg parameter.

They are separate calls so you, the programmer, can have a chance to avoid the message translation provided by TranslateMessage.

like image 45
Jorge Ferreira Avatar answered Sep 22 '22 11:09

Jorge Ferreira