Why exactly TranslateMessage

I am trying to understand "The Message Loop". Here's what it looks like:

MSG msg = { }; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } 

So far I understand (at least I hope) with the concept. When a user interacts with a Windows application using the keyboard and mouse, these events are converted into the corresponding messages by the corresponding device drivers and sent to the system message queue.

The OS deletes messages from the queue one by one and analyzes each of them to send them to the corresponding application thread queue, which is responsible for creating the destination window.

Now in my application

 MSG msg; GetMessage(&msg, NULL, 0, 0); 

removes the message from the stream-specific message queue and populates the MSG structure.

But they say that TranslateMessage converts virtual keystrokes to characters and sends them back to the call flow message queue.

DispatchMessage directs the OS to invoke the Windows procedure of the corresponding target window.

Two doubts:

1) What is the exact functionality of TranslateMessage, is it just to translate virtual keystrokes to symbolic messages (I assume that virtual keystrokes are keystrokes other than alphabets and numbers), if the character message is sent back to the queue, is the loop not broken?

2) What about mouse events? Are they shipped directly?

+4
source share
2 answers

Yes, it doesn't make sense that you have to call TranslateMessage () when your message loop looks like this. But this is not what the canonical Petzold message loop looks like:

 while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } 

Accelerators are important here, otherwise called "short keys." Your program wants to answer them no matter which window has focus. As F1 shows the program help file, no matter which control has focus. You do not want to write code that subclasses of each control window recognize F1.

So, if this is a short key, then you do not want to call TranslateMessage. The key should not produce a WM_CHAR message if the key matches the input type. That is why this is a separate challenge.

+8
source

Shortly speaking,

  • Yes, it's just for translating virtual keystrokes to symbolic messages.
  • Yes, they are sent directly, see this blog in msdn

See details

+3
source

Source: https://habr.com/ru/post/1436127/


All Articles