Converting wParam?? Edit[Now Vector STL help]

Started by
11 comments, last by Altemia 13 years, 5 months ago
Hey I'm new :),

I'm trying to take character input from a keyboard and display it in the window of an OpenGL Scene. I can quite happily display text to the screen but not words input by the user :(. From what i've seen and read online I need to convert the wParam input into a char variable I can pass into my text class. How would I go about this or is there some literature I can be pointed to that explains the process. I've read Article #18 on Nehe which covers this [http://nehe.gamedev.net/data/articles/article.asp?article=18] but I didn't understand the organization. Any help appreciated.

Dave,

[Edited by - Altemia on November 16, 2010 1:48:18 PM]
Advertisement
Which window message are you trying to process? If you're messing with WM_KEYDOWN then you're in for a headache. If you're using WM_CHAR then it really is as easy as char c = static_cast<char>(wParam). Unless, of course, that it isn't an ASCII character, in which case you'll need to use wide characters.
Quote:Original post by SiCrane
Which window message are you trying to process? If you're messing with WM_KEYDOWN then you're in for a headache. If you're using WM_CHAR then it really is as easy as char c = static_cast<char>(wParam). Unless, of course, that it isn't an ASCII character, in which case you'll need to use wide characters.


I was indeed planning on using WM_KEYDOWN but I don't want a headache. I'm curious are there benefits to using WM_KEYDOWN over WM_CHAR, if not why do people use WM_KEYDOWN for characters?

In regard to WM_CHAR and c = static_cast<char>(wParam). I expect all input will be an ASCII character as I only want to be able to input sentences.

Is it simply a case that printing the c variable will print the character entered?

Thanks for your help thus far :)

Edit: Ok I used WM_CHAR as suggested and I can now display characters the user has input with the keybaord. Why does the NEHE article user WM_KEYDOWN? Surely the simplicity of what SiCrane suggested would make it more popular?

[Edited by - Altemia on November 12, 2010 6:24:27 AM]
I can't on specifically why the author of that article uses WM_KEYDOWN. I do know that a lot of people don't use WM_CHAR simply because they don't realize that it exists. It's pretty easy to use search engines to find WM_KEYDOWN and once people find WM_KEYDOWN they don't go on to see if there's another window message better suited for their purpose.
I haven't read through the NEHE tutorial, but I'm not sure if it uses the input for text. I guess it shows a more general input handling, since WM_CHAR is only good for text input, but (AFAIK, I'm not sure about this one) not good for arrow keys for example (simply the WM_CHAR is not sent), or function keys (F1...F12) etc.

Clarify someone please!
Quote:Original post by szecs
I haven't read through the NEHE tutorial, but I'm not sure if it uses the input for text.

Quote:The very first complete sentence of the linked article
This class makes it possible to input strings, chars and integers in OpenGL while working with the Windows API.

Sounds like text to me.
I have a further question, i'm using the vector STL to store the text I input but for some reason the entire list seems to be overwritten eveyr time I enter new text.

Any idea why this would happen (perhaps you've noticed it before)?

Or can you spot my mistake?

In header and made public
      std::vector<const char *> List;        std::vector<const char *>::iterator iter;


In the render function
	if(textInput->IsOver())			// Is The Process Over?	{		textInput->GetStr(k);		// If So, Put The Input In A Variable		List.push_back(k);		textInput->BeginProcess(PR_GETSTRING); //Tell the class what type of input were expecting	}	if(!List.empty())	{		for(iter=List.begin(); iter!=List.end(); iter++) 		{			_text->Print(true,tVector3(BoxPos.x+5,BoxPos.y-20,0),"Using List :- %s",*iter);// Display the inputted text		}	}
Holding a vector of const char * is almost never what you really want to do. Try using a vector of std::string objects instead.
The GetStr function returns a char* so I unfortunetly have to use char not string. This is due to the way I create the words by adding each character into an array
Well, re-write it in terms of std::string. This will probably fix bugs you didn't know you had.

In the mean time, you can construct a std::string from a char pointer, so you can change the vector to <string> and it will work (provided you are correctly managing memory for "k").

This topic is closed to new replies.

Advertisement