Difference between revisions of "love.textinput (简体中文)"

Line 29: Line 29:
 
end
 
end
 
</source>
 
</source>
 +
 +
打印用户输入的文字,并在按下退格键(backspace)时删除一个文字。
 +
 +
<source lang="lua">
 +
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
 +
</source>
 +
 +
== 其他相关 ==
 +
* [[parent::love]]
 +
* [[love.keypressed]]
 +
* [[love.keyboard.setTextInput]]
 +
* [[love.keyboard.hasTextInput]]
 +
* [[utf8]]
 +
* [https://wiki.libsdl.org/Tutorials/TextInput textinput vs keypressed]
 +
[[Category:Callbacks]]
 +
{{#set:Description=Called when text has been entered by the user.}}
 +
{{#set:Subcategory=Keyboard}}
 +
 +
== 其他语言 ==
 +
{{i18n|love.textinput}}

Revision as of 14:48, 18 August 2022

函数

概要

love.textinput( text )

参数

string text
UTF-8 文本

Returns

无返回


注释

在 Android 和 iOS 上,默认禁用 textinput;调用love.keyboard.setTextInput来启用它。

例子

记录并打印用户输入的文本。

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

打印用户输入的文字,并在按下退格键(backspace)时删除一个文字。

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

其他相关


其他语言