Advertisement

NEHE KeyBoard process Confusing

Started by May 04, 2006 04:02 AM
3 comments, last by Love3DGraphiks 18 years, 6 months ago
If u have ever take a look at Nehe Tutorial ,then I wanna ask some question IF this Code below is hard to see you can look at source at Nehe Page ,coz it is the same for every tutorial ,and take a look at WndProc And WinMain Nehe Define Keyboard Buffer 256 character key[256] rite?? In WindProc they Code like this LRESULT CALLBACK WndProc(HWND hWnd, // Handle for this window UINT uMsg, // Message for this window WPARAM wParam, // Additional message information LPARAM lParam) // Additional message information { switch (uMsg) // Check for windows messages { case WM_ACTIVATE: // Watch for window activate message { if (!HIWORD(wParam)) // Check minimization state { active = true; // Program is active } else { active = false; // Program is no longer active } return 0; // Return to the message loop } case WM_SYSCOMMAND: // Intercept system commands { switch (wParam) // Check system calls { case SC_SCREENSAVE: // Screensaver trying to start? case SC_MONITORPOWER: // Monitor trying to enter powersave? return 0; // Prevent from happening } break; // Exit } case WM_CLOSE: // Did we receive a close message? { PostQuitMessage(0); // Send a quit message return 0; // Jump back } case WM_KEYDOWN: // Is a key being held down? { keys[wParam] = true; // If so, mark it as TRUE return 0; // Jump back } case WM_KEYUP: // Has a key been released? { keys[wParam] = false; // If so, mark it as FALSE return 0; // Jump back } case WM_SIZE: // Resize the OpenGL window { ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord = Width, HiWord = Height return 0; // Jump back } } // Pass all unhandled messages to DefWindowProc return DefWindowProc(hWnd,uMsg,wParam,lParam); } /---------------------------------------------------------- WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; // Windows message structure bool done = false; // Bool variable to exit loop // Ask the user which screen mode they prefer if (MessageBox(NULL,"Would you like to run in fullscreen mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO) { fullscreen = false; // Windowed mode } // Create our OpenGL window if (!CreateGLWindow("NeHe's Masking Tutorial",640,480,16,fullscreen)) { return 0; // Quit if window was not created } while(!done) // Loop that runs while done = FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is there a message waiting? { if (msg.message == WM_QUIT) // Have we received a quit message? { done = true; // If so done = TRUE } else // If not, deal with window messages { TranslateMessage(&msg); // Translate the message DispatchMessage(&msg); // Dispatch the message } } else // If there are no messages { // Draw the scene. Watch for ESC key and quit messages from DrawGLScene() if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was there a quit received? { done = true; // ESC or DrawGLScene signalled a quit } else // Not time to quit, update screen { SwapBuffers(hDC); // Swap buffers (double buffering) if (keys[' '] && !sp) // Is space being pressed? { sp = true; // Tell program spacebar is being held scene = !scene; // Toggle from one scene to the other } if (!keys[' ']) // Has spacebar been released? { sp=FALSE; // Tell program spacebar has been released } if (keys['M'] && !mp) // Is M being pressed? { mp = true; // Tell program M is being held masking = !masking; // Toggle masking mode OFF/ON } if (!keys['M']) // Has M been released? { mp = false; // Tell program that M has been released } if (keys[VK_F1]) // Is F1 being pressed? { keys[VK_F1] = false; // If so make key FALSE KillGLWindow(); // Kill our current window fullscreen = !fullscreen; // Toggle fullscreen / windowed mode // Recreate our OpenGL window if (!CreateGLWindow("NeHe's Masking Tutorial",640,480,16,fullscreen)) { return 0; // Quit if window was not created } } } } } // Shutdown KillGLWindow(); // Kill the window return (msg.wParam); // Exit the program } //--------------------------------------------------------------------------- so I wanna ask : When We press a key or release a key ,system will send u a message with WM_KEYUP or WM_KEYDOWN rite?? and Nehe Process Key[wParam]=true if WM_KEYDOWN and Key[Wparam]=False if WM_KEYUP (release rite??) now it's PeekMessage turn in Function WinMain PeekMessage will return 0 if no Message and then it will pass the control to the else {.........} and the Code in section Else will determine if Key['Some key']is true or false rite ?? Now here is my confuse : When I press a key System send message WM_KEYDOWN and Key[Wparam] is marks as true but after I press a key I should Release that key ,and then Key[wpram] (That same key u have pressed) is marks =False so Key[Somekey] in section Else below PeekMessage func is always marks as FALSE u look at back at the code ,if(key['M']) then do something ,I wonder why I pressed M ,and it still affact that code for Example normally if I press M it will toggle ENABLE(GL_BLEND) , but in this situation Key['M'] never marks as true ,but Why it still affact GL_BLEND
lol *.

Correct if i'm wrong, but if you want turn on your Blend one time per pressing a key. you could write like this:
if(keys['M']){   glEnabale(GL_BLEND);   keys['M']=false;}
Advertisement
Thanks for Replying

but I think the code
{     glEnable(GL_BLEND);     key['M']=false;}


is never affact (means it never be excuted] coz keys['M'] always Hold FALSE


Like I said in the first Post above ,when u Press akey system send WM_KEYDOWN
and WndProc assign Keys[wParam]=TRUE , then after that a release key then system send another message with MW_KEYUP and WndProc assign[wParam]=FALSE

Detail Example:

I press key M then system send WM_KEYDOWN and wParam will hold ASCII of M
Keys['M']=TRUE ,and then I release key M system send WM_KEYUP and wparam hold ASCII of M ,Keys['M']=FALSE

see? Key['M'] al ways FALSE when u press any key on the keyboard

I may be wrong but I need some explain thanks
I may be wrong, but I think the WM_KEYDOWN message gets pushed to the stack, and is processed before the WM_KEYUP message. So everything in the conditional WM_KEYDOWN block is executed before the WM_KEYUP is even considered.

I think what you're confused about is you believe that the keys[pressedKey] will immediately revert to false after the key is released, but because the the keyup message is lower priority in the stack, it is processed after the keydown.
Thanks so much ,after I check sum more references about Message Queue Synchronizing , and I found out that it same same from what you thought and told me but not exactly but what u imagined is also working

may be u would Like to know exactly about it , i'll explain detail about it coz it seemed the previous you posted you were not sure enough

Here's the deal
I translate to english ,I hope u understand coz my english is not good
Section VI.1.3 : Message Queue and Synchronizing

When the user push and release a key from the keyboard ,Windows and device drives will translate the Scancode of keyboard to Messages( keyboard message) .But, these messages will not immediately put into Application's Message Queue that has focus ,instead Windows will temporarily put these messages into another Message Queue that is : System Message Queue

System Message Queue is the unique Message Queue that managed by Windows for preprocessing messages .And then Windows will take the messages from System Message Queue in turn to check and only put message into Application's Message Queue that has focus whenever the the Application already processed the previous mouse or key board messages (thats what you told me thay have priority :) thanks)

This topic is closed to new replies.

Advertisement