Text Input: Creating words by entering and showing on screen

Hi all,

I’m extremely new to oF and have very little coding experience through Processing (super beginner code-wise). I’ve looked everywhere for several days now and I can’t even seem to find any proper reference so if someone can please help guide me, I’d be most grateful.

I’m currently using some code kindly posted online here: http://gussiefinknottle.wordpress.com/c-…-rameworks/

and mashing it up with the soundPlayerExample.

Now that I’ve got that working, how do I:

Code so that I can type out a word on the keyboard, press enter, then have the word appear on the screen?

Or alternatively, show the word being typed out in real time onscreen and press enter so that it saves somewhere on the screen.

All the examples I’ve found only show strings already written out in the body of the code.
The soundPlayer example shows how the letter can be highlighted, but doesn’t give a clue on how it can stay.

I hope this makes sense to someone.

Thank you thank you thank you!!

Just to clarify a bit:

I simply want to type words and have them displayed on screen.

I’d like to combine a keypressed and draw function together if possible - or find an alternative.

So something like:

void testApp::keyPressed (int key){
if (key == ‘a’){
theFont.drawString(“a”);

and then something to string them together like:

string str =""’’
str += char(letter);
theFont.drawString(str, 100, 250);

Please pardon the examples. The point is that I’m not familiar with code/c++/openframeworks language well enough yet.

Thanks again.

you’d want to define a string in testApp.h
class testApp {
public:

string str;
};

then in testApp::setup() { you want to initialize it as an empty string: str = “”;

then in keyPressed() you add a character to it. (also you want to take into account the backspace key)

void testApp::keyPressed(int key) {
if(key==8 && str.size()>0) { // backspace
str = str.substr(0, str.size()-1); // delete one character
} else {
str.append (1, (char)key );
}
}

then in testApp::draw(), just draw the string

theFont.drawString(str, 100, 250);

you don’t want to do any drawing outside of the draw() method.

I think that’s it.

Sweet! Just tried it and it works! Thank you so much for your help! One hurdle jumped, some more to go. Thanks again!

Hi guys,

I’m quite new with OF and I’m working on this code:
I want to type on the keyboard and print the text on the screen. I started from OF examples and it works fine:

void testApp::keyPressed(int key){

if(key == OF_KEY_DEL || key == OF_KEY_BACKSPACE){
typeStr = typeStr.substr(0, typeStr.length()-1);
}
else if(key == OF_KEY_RETURN ){
typeStr += “\n”;
}else{
if( bFirst ){
typeStr.clear();
bFirst = false;
}

typeStr.append(1, (char)key);

}
}

but I have problems with accents and special characters. It doesn’t print the right char
f.e. if I press:
“ò” it print “Ã2”
“ç” -> “ç”
“à” -> “Ô
“ù” -> “Ã1”

Can you help me? any ideas how to solve this problem?
Thank you