c++ - Better Way to programmaticly detect a key press? -
c++ - Better Way to programmaticly detect a key press? -
i'm using this:
while (1) { if (getasynckeystate(vk_f1)) { //do } }
to observe if user presses key, in case f1. i've found eats cpu usage quite bit, question is, there improve way observe key presses?
the improve way of doing using wndproc()
. utilize standard wm_keydown
/wm_keyup
messages handle keyboard input. here example:
lresult callback wndproc( hwnd hwnd, uint umsg, wparam wparam, lparam lparam ) { switch ( umsg ) { case wm_close: destroywindow(hwnd); break; case wm_destroy: postquitmessage(0); break; case wm_keydown: if ( wparam == vk_f1 ) { // here homecoming 0l; } break; } homecoming defwindowproc( hwnd, umsg, wparam, lparam ); } int winapi winmain(hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) { wndclassex wc; hwnd hwnd; msg msg; //step 1: registering window class wc.cbsize = sizeof(wndclassex); wc.style = 0; wc.lpfnwndproc = wndproc; // lots of other attrs ... wc.lpszclassname = g_szclassname; wc.hiconsm = loadicon(null, idi_application); if(!registerclassex(&wc)) homecoming 0; // step 2: creating window hwnd = createwindowex( ... g_szclassname, ...); if(hwnd == null) homecoming 0; showwindow(hwnd, ncmdshow); updatewindow(hwnd); // step 3: message loop while(getmessage(&msg, null, 0, 0) > 0) { translatemessage(&msg); dispatchmessage(&msg); } homecoming msg.wparam; }
c++ windows keypress
Comments
Post a Comment