Skip to main content
gamedev.net
🔒 Locked

NeHe + Keyboard

Started by brain21 Jan 20, 2006 at 10:31 AM 3 replies 1k views
Original Post
brain21
brain21
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
Skeleton_V@T
Skeleton_V@T
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.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
brain21
brain21
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
iNsAn1tY
iNsAn1tY
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...
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
brain21
brain21
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.