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

(函数)
m (Fix parent)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
{{newin|[[0.9.0]]|100|type=variant}}
 
{{newin|[[0.9.0]]|100|type=variant}}
 
== 函数 ==
 
== 函数 ==
 +
当用户输入文本时调用。例如,如果在键盘上按下 shift + 2,则会生成“@”。
 
=== 概要 ===
 
=== 概要 ===
 
<source lang="lua">
 
<source lang="lua">
Line 65: Line 66:
  
 
== 其他相关 ==
 
== 其他相关 ==
* [[parent::love]]
+
* [[parent::love (简体中文)]]
 
* [[love.keypressed]]
 
* [[love.keypressed]]
 
* [[love.keyboard.setTextInput]]
 
* [[love.keyboard.setTextInput]]

Latest revision as of 20:28, 12 January 2023

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

函数

当用户输入文本时调用。例如,如果在键盘上按下 shift + 2,则会生成“@”。

概要

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

其他相关


其他语言