c++ - Why PeekMessage doesn't call keyboard hook proc on message posted by PostMessage? -
i have app handle keyboard input via keyboard hooks. sets hook setwindowshookex(wh_keyboard,...).
when peekmessage in message processing loop receives usual wm_keyup , wm_keydown messages real hardware keyboard, calls callback , fine, ignores messages sent postthreadmessage (and other messaging function). doesn't call hook procedure!
there piece of terrible code (mostly auto-generated visual studio) below illustrate behavior. there window message-processing loop , thread, sending wm_keydown messages every 1 second. calls of keyboard callback writes log records console window, , can see them, when press key on keyboard, there no records messages sent second thread!
i cannot change main application implementation. in many reasons should work via keyboard hooks. how can make getmessage or peekmessage handle keyboard messages handles messages hardware keyboard? great help, i've spent whole week problem!
i've tested on windows 8.1 64-bit 32-bit application.
here code:
#include "windows.h" #include <iostream> #include <fstream> using namespace std; #define max_loadstring 100 hinstance hinst; tchar *sztitle = text("title"); tchar *szwindowclass = text("hookedwindowclass"); lresult callback keyboardcallback( _in_ int code, _in_ wparam wparam, _in_ lparam lparam ) { wcout << l"keyboardcallback " << endl; msg *msg = (msg*)lparam; wcout << l"kbhook " << code << l" wparam " << hex << wparam << l" lparam " << lparam << dec << endl; return callnexthookex(null,code,wparam,lparam); } atom myregisterclass(hinstance hinstance); bool initinstance(hinstance, int); lresult callback wndproc(hwnd, uint, wparam, lparam); int_ptr callback about(hwnd, uint, wparam, lparam); dword mainthreadid = 0 ; hwnd window = 0; dword threadfn(void*) { while(true) { if(!postthreadmessage(mainthreadid,wm_keydown,0xd,0x1c0001)) { cout << "error posting message " << hex << mainthreadid << " error " << getlasterror() << endl; } sleep(1000); } } int apientry winmain(_in_ hinstance hinstance, _in_opt_ hinstance hprevinstance, _in_ lpstr lpcmdline, _in_ int ncmdshow) { unreferenced_parameter(hprevinstance); unreferenced_parameter(lpcmdline); msg msg; myregisterclass(hinstance); if (!initinstance (hinstance, ncmdshow)) { return false; } allocconsole(); wfstream console_out("conout$"); wcout.rdbuf(console_out.rdbuf()); hhook hhook = setwindowshookex(wh_keyboard,keyboardcallback,getmodulehandle(null),getcurrentthreadid()); mainthreadid = getcurrentthreadid(); createthread(null,0,(lpthread_start_routine)threadfn,null,0,null); while (getmessage(&msg, null, 0, 0)) { switch (msg.message) { case wm_keydown: wcout << l"wm_keydown" << l" wparam " << hex << msg.wparam << l" lparam " << msg.lparam << dec << endl; break; default: break; } translatemessage(&msg); dispatchmessage(&msg); } return (int) msg.wparam; } atom myregisterclass(hinstance hinstance) { wndclassex wcex; wcex.cbsize = sizeof(wndclassex); wcex.style = cs_hredraw | cs_vredraw; wcex.lpfnwndproc = wndproc; wcex.cbclsextra = 0; wcex.cbwndextra = 0; wcex.hinstance = hinstance; wcex.hicon = null; wcex.hcursor = loadcursor(null, idc_arrow); wcex.hbrbackground = (hbrush)(color_window+1); wcex.lpszmenuname = null; wcex.hiconsm = null; wcex.lpszclassname = szwindowclass; return registerclassex(&wcex); } bool initinstance(hinstance hinstance, int ncmdshow) { hwnd hwnd; hinst = hinstance; hwnd = createwindow(szwindowclass, sztitle, ws_overlappedwindow, cw_usedefault, 0, cw_usedefault, 0, null, null, hinstance, null); if (!hwnd) { return false; } showwindow(hwnd, ncmdshow); updatewindow(hwnd); window = hwnd; return true; } lresult callback wndproc(hwnd hwnd, uint message, wparam wparam, lparam lparam) { int wmid, wmevent; paintstruct ps; hdc hdc; switch (message) { case wm_paint: hdc = beginpaint(hwnd, &ps); endpaint(hwnd, &ps); break; case wm_destroy: postquitmessage(0); break; case wm_quit: exitprocess(0); default: return defwindowproc(hwnd, message, wparam, lparam); } return 0; }
Comments
Post a Comment