love.textinput (Español)

Available since LÖVE 0.9.0
This function is not supported in earlier versions.

Llamado cuando texto ha sido ingresado por el usuario. Por ejemplo si 'AltGr + 2' es presionado en un teclado Español, el texto "@" sera generado.

Función

Sintaxis

love.textinput( text )

Argumentos

string text
El texto en formato UFT-8.

Retornos

Nada.

Notas

Aunque las cadenas de texto Lua pueden almacenar texto Unicode codificado en UTF-8 muy bien, muchas funciones en la biblioteca de cadenas de Lua no tratarán el texto como creerias esperar. Por ejemplo, #text (and string.len(text)) Retornara el numero de bytes en la cadena de texto, en vez de la cantidad de carácteres unicode. La wikipedia lua y una presentación por uno de los creadores de Lua entrega mas información en el tema con explicaciones, y algunos tips.

La libreria utf8 puede ser usado para operar en texto codificado en UFT-8 (Como los argumentos dados en esta función.)

En Android y iOS, textinput esta deshabilitado por defecto; Ejecuta love.keyboard.setTextInput para habilitarlo.

Ejemplos

Capturar e imprimir lo que el usuario escribe.

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

function love.textinput(t)
    text = text .. t
end

function love.draw()
    love.graphics.printf(text, 0, 0, love.graphics.getWidth())
end

Imprime el texto que el usuario escribe, y borrarlo cuando 'backspace' es presionado.

local utf8 = require("utf8")

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

    -- enable key repeat so backspace can be held down to trigger love.keypressed multiple times.
    love.keyboard.setKeyRepeat(true)
end

function love.textinput(t)
    text = text .. t
end

function love.keypressed(key)
    if key == "backspace" then
        -- get the byte offset to the last UTF-8 character in the string.
        local byteoffset = utf8.offset(text, -1)

        if byteoffset then
            -- remove the last UTF-8 character.
            -- string.sub operates on bytes rather than UTF-8 characters, so we couldn't do string.sub(text, 1, -2).
            text = string.sub(text, 1, byteoffset - 1)
        end
    end
end

function love.draw()
    love.graphics.printf(text, 0, 0, love.graphics.getWidth())
end

Véase también


Other Languages