Page 1 of 1

Problem using accents

Posted: Wed Jan 09, 2013 6:29 am
by Zamp
Hi lövers,

I'm trying to build a gui and i have a problem using accented letters in user input.

Considering this code,

Code: Select all

function love.load()
    text = "Type away! -- "
end

function love.keypressed(key, unicode)
    -- ignore non-printable characters (see http://www.ascii-code.com/)
    if unicode > 31 and unicode < 127 then
        text = text .. string.char(unicode)
        print(unicode)
    end
end

function love.draw()
    love.graphics.printf(text, 0, 0, 800)
end
accented letters (é,è...) never print, neither the unicode in the console.

I'm using a french keyboard.

I hope someone could help me to fix this :P

Thanks !

Re: Problem using accents

Posted: Wed Jan 09, 2013 7:13 am
by Santos
Does it work without the upper limit unicode limit? Edit: Nope! Thanks for explaining this Boolsheet! :)

Re: Problem using accents

Posted: Wed Jan 09, 2013 9:13 am
by Saegor
Zamp wrote:Hi lövers
hi löver !

french accents (é, è, ç) never worked for me, nor numbers associated by the physical key

so, for exemple, i can't type a "2" because it's up the "é"

Lua lack unicode support so... no numbers for french people.
try to make menu without accents and games without using the "2", "7", "9"... key :?

Re: Problem using accents

Posted: Wed Jan 09, 2013 9:15 am
by Boolsheet
Your suggestion of removing a vital part of the code will lead to errors. There's generally a reason why code was written in a certain way, though bad code exists too.

string.char takes an integer from 0 to 255 and returns a string with the character representation of that number. This usually maps the number to the byte (depends on the locale which should default to C).

All LÖVE functions (with one exception) take UTF-8 encoded strings. UTF-8 is compatible with ASCII (a 7-bit encoding) and the limitation in the example function makes sure it stays in the printable character range. All characters above the value 127 use two or more bytes when encoded with UTF-8.

If you want to add characters outside of ASCII to a string, you'll have to encode them in UTF-8. LÖVE does not offer functions for this, but there should be a few pure Lua libraries that do it.

Re: Problem using accents

Posted: Thu Jan 10, 2013 6:06 am
by Zamp
Thanks you guys for your answers, and Boolsheet for explaining this problem :)