本文是关于TranslateMessage官方文档的相关翻译,拿走不谢。
TranslateMessage函数将虚拟键消息转换成字符消息。该字符消息又被发送给对应线程(调用TranslateMessage的线程)的消息队列,当线程再次调用GetMessage函数或PeekMessage函数获取消息的时候被读取。
函数原型:
BOOL TranslateMessage(
const MSG *lpMsg
);
参数说明:
lpMsg:指向由GetMessage或PeekMessage从调用线程消息队列获取的MSG信息结构。
返回值:
如果消息被转换(相应的字符消息被发送到调用线程的消息队列),返回值将为非零。
如果消息是 WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, 或 WM_SYSKEYUP,返回非零值,转换情况将被忽略。
如果消息没有转换(即,相应的字符消息没被发送到调用线程的消息队列中),返回值将是零。
注意事项:
TranslateMessage函数不修改由参数lpMsg指向的消息结构。
消息WM_KEYDOWN和WM_KEYUP组合产生一个WM_CHAR或WM_DEADCHAR消息。消息WM_SYSKEYDOWN和WM_SYSKEYUP组合产生一个WM_SYSCHAR或 WM_SYSDEADCHAR 消息。
TtanslateMessage函数仅为那些由键盘驱动器映射为ASCII字符的键产生WM_CHAR消息。
如果应用程序由于其它用途而需要处理虚拟键消息,则不应调用TranslateMessage函数。例如,当应用程序的TranslateAccelerator函数返回一个非零值时,它就不应该调用TranslateMessage函数。注意,应用程序负责检索和调度输入消息到该对话框,大多数程序使用主循环来完成这一工作。然而,要让用户可以通过键盘来进行控制操作,那么该程序必须调用IsDialogMessage函数。
//原文如下:
The TranslateMessage functiontranslates virtual-key messages into character messages. The character messagesare posted to the calling thread's message queue, to be read the next time thethread calls the GetMessage or PeekMessage function.
Syntax
BOOL TranslateMessage(
const MSG *lpMsg
);
Parameters
lpMsg
[in] Pointer to an MSGstructure that contains message information retrieved from the calling thread'smessage queue by using theGetMessage or PeekMessage function.
Return Value
If the message istranslated (that is, a character message is posted to the thread's messagequeue), the return value is nonzero.
If the message isWM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the return value isnonzero, regardless of the translation.
If the message is nottranslated (that is, a character message is not posted to the thread's messagequeue), the return value is zero.
Remarks
The TranslateMessage functiondoes not modify the message pointed to by thelpMsg parameter.
WM_KEYDOWN and WM_KEYUPcombinations produce a WM_CHAR or WM_DEADCHAR message.WM_SYSKEYDOWN andWM_SYSKEYUP combinations produce a WM_SYSCHAR or WM_SYSDEADCHAR message.
TranslateMessage producesWM_CHARmessages only for keys that are mapped to ASCII characters by the keyboarddriver.
If applications processvirtual-key messages for some other purpose, they should not callTranslateMessage. For instance,an application should not callTranslateMessageif the TranslateAccelerator function returns a nonzero value. Note that theapplication is responsible for retrieving and dispatching input messages to thedialog box. Most applications use the main message loop for this. However, topermit the user to move to and to select controls by using the keyboard, theapplication must call IsDialogMessage. For more information, see Dialog BoxKeyboard Interface.
Windows 95/98/Me: TranslateMessage is supportedby the Microsoft Layer for Unicode (MSLU). To use this, you must add certainfiles to your application, as outlined in Microsoft Layer for Unicode onWindows 95/98/Me Systems.