Can I call TranslateMessage inside the message callback?

我的未来我决定 提交于 2019-12-12 03:57:33

问题


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?

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);

回答1:


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.

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.



来源:https://stackoverflow.com/questions/15690920/can-i-call-translatemessage-inside-the-message-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!