Problem using accents

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Zamp
Prole
Posts: 14
Joined: Sun Oct 07, 2012 1:30 pm

Problem using accents

Post 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 !
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Problem using accents

Post by Santos »

Does it work without the upper limit unicode limit? Edit: Nope! Thanks for explaining this Boolsheet! :)
Last edited by Santos on Wed Jan 09, 2013 9:47 am, edited 1 time in total.
User avatar
Saegor
Party member
Posts: 119
Joined: Thu Nov 08, 2012 9:26 am
Location: Charleroi

Re: Problem using accents

Post 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 :?
Current work : Isömap
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Problem using accents

Post 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.
Shallow indentations.
Zamp
Prole
Posts: 14
Joined: Sun Oct 07, 2012 1:30 pm

Re: Problem using accents

Post by Zamp »

Thanks you guys for your answers, and Boolsheet for explaining this problem :)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests