Jump to content
  • Advertisement
Sign in to follow this  
brain21

NeHe + Keyboard

This topic is 4563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everybody. I ran into a problem with my project. I am using the NeHe base code. I am trying to get a console window happening, but I can't get the keyboard input right. The NeHe code just gives me an array of bool telling me what keys have been pressed. Is there a way to get the actual key that has been pressed instead of looping through that array and checking everyone? Also, this array does not tell me if the key is Caps or not. I was thinking of using DirectInput or SDL for the input, but would much rather find a solution using the NeHe base code. Any suggestions? Brain21

Share this post


Link to post
Share on other sites
Advertisement
Are you using Windows ?. If so, you may try GetKeyboardState () API function. It updates the key states (including mouse button's states) into an array of 256 unsigned char.

You may replace the code which updates the individual key states by one line which invokes this function, although I have that function invoked in the main game loop. Try not to mess with the window procedure as much as you can to increase portability.

Share this post


Link to post
Share on other sites
I am using Windows. The Nehe base code already updates an array of keys pressed, but that does not help me. I want to add the pressed key to a string that I show on the screen. I could loop though the array and find the key that was pressed and append that to the string, but that would take lots of time. I thought that there would be an easier way of doing it.

brain21

Share this post


Link to post
Share on other sites
Looping through the array every cycle would be very expensive in terms of processor time, and completely unnecessary. You need to listen for WM_CHAR messages in your Windows procedure. The wParam parameter contains the character code, which you can cast to a char and append to your string. The Caps Lock problem is handled for you by Windows; the character code you're sent will be either uppercase or lowercase, depending on the state of the Caps Lock key. You also get some very useful information in the lParam, like the repeat count and the previous state of the key. Have a look in the MSDN for it. Just don't forget to call TranslateMessage in your message loop before you call DispatchMessage, otherwise things won't go smoothly...

Share this post


Link to post
Share on other sites
Thanks iNsAn1tY, That work exactly how I needed it to! I don't know why I was missing that TranslateMessage in my loop.

Thanks again!

Brain21

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!