Text input, problems with accents and special characters

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

when you say, “It doesn’t print the right char” – do you mean the console (using cout) or using ofTrueTypeFont. If it’s with a font, you might need to look at (a) if you are loading the font with extended character set and (b) if you need to look at some of the examples on this forum, and on ofxAddons for addons, of people who have gotten fonts to work with foreign languages (ie, wide strings, utf-8, etc).

http://ofxaddons.com/repos/128

on thing to consider is that with some character entries, you are actually getting utf-8 entities, so there’s actually more then one key press for certain keys. For example, if I do this:

  
  
void testApp::keyPressed(int key){  
  
      
    cout << key << endl;  
      
  

and I turn on a foreign input on my keyboard (I’m on mac, and just Jianyi (chinese input), I get this for each key press:

  
  
230  
137  
139  
  
230  
137  
139  
  

where the character is coming in as multiple key presses (utf-8 uses variable length per character, including sometimes "modifier’ values). I’m pressing one key, but you see three values. In this case, you have to handle the string properly, and certain functions like strlen, etc will break and alot of OF string related functions aren’t fully utf-8 friendly. I’ve had this issue in the past with foreign keyboards, and if this is your issue, I can try to dig up my code, which involved looking for utf-8 modifiers, etc.

hope this helps!
zach

Also Christopher Baker just posted an alternative Unicode addon:
http://ofxaddons.com/repos/672

he’s got a pretty clear example app:
https://github.com/bakercp/ofxUnicode/blob/master/example/src/testApp.cpp