我可以在消息回调内调用TranslateMessage吗? [英] Can I call TranslateMessage inside the message callback?
问题描述
我没有运行规范的消息循环,所以有什么方法可以在我的消息proc处理程序中调用TranslateMessage(或其等效项)?
I don't have the canonical message loop running, so is there a way I can call TranslateMessage (or its equivalent) inside my message proc handler?
基本上,我需要WM_CHAR
消息,除非可以调用TranslateMessage,否则不会获得这些消息.目前,我已经设置了消息处理程序,但没有消息循环.
Basically I need WM_CHAR
messages and unless I can call TranslateMessage I'm not going to get those. Currently I have the message proc setup, but no message loop.
// Static window function called by Windows for message processing. Implementation
// delegates message processing to MemberWndProc.
LRESULT CALLBACK FxPlayerTiny::WindowsMsgStatic(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
msg = PeekMessage(&msg, HWnd, WM_KEYFIRST, WM_KEYLAST, PM_NOREMOVE);
if (msg type is key down){
TranslateMessage(&msg);
//DispatchMessage(&msg); -- needed?
}
else process msg normally
}
我的消息处理程序是消息的第一个输入点,其设置方式如下:
My message proc handler is the first point of entry of messages, being setup in the following manner:
WNDCLASSEX wc;
wc.lpfnWndProc = WindowsMsgStatic;
....
RegisterClassEx(&wc);
推荐答案
在某些时候,为了获得排队的消息,您必须调用GetMessage
或PeekMessage
之类的函数.这些函数产生MSG
对象,而这些MSG
对象必须传递给TranslateMessage
和DispatchMessage
.
At some point, in order to get a queued message, you must call a function like GetMessage
or PeekMessage
. Those functions yield MSG
objects and it is those MSG
objects that you must pass to TranslateMessage
and DispatchMessage
.
在问题原始版本的代码中,您试图调用TranslateMessage
和DispatchMessage
为时已晚.您可以在窗口proc中调用它们.您应该在第一次收到MSG
对象的地方调用它们.换句话说,在调用PeekMessage
或GetMessage
之后立即调用TranslateMessage
和DispatchMessage
.
In the code in the original version of the question, you are trying to call TranslateMessage
and DispatchMessage
too late. You call them inside your window proc. You should call them at the point where you first receive the MSG
object. In other words, call TranslateMessage
and DispatchMessage
straight after the call to PeekMessage
or GetMessage
.
这篇关于我可以在消息回调内调用TranslateMessage吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!