{{newin (日本語)|[[0.9.0 (日本語)|0.9.0]]|090|type=関数}}
ユーザによりテキストが入力されたときに呼ばれます。例えば、米国英語配列のキーボードで [Shift]+[2] を押すと、 "@" のテキストが生成されます。
== 関数 ==
=== 概要 ===
<source lang="lua">
love.textinput( text )
</source>
=== 引数 ===
{{param (日本語)|string|text|UTF-8 で符号化された Unicode 候補テキスト。}}
=== 返値 ===
ありません。

== 注釈 ==
Lua は UTF-8 でエンコードされた Unicode テキストを正確かつ良好に文字列として記憶できますが、ほとんどの Lua 文字列ライブラリ関数はテキストを要求通りに処理できません。例えば、 <code>#text</code> (および <code>string.len(text)</code> ) は Unicode 文字数ではなく、文字列の''バイト''数を返します。[http://lua-users.org/wiki/LuaUnicode Lua wiki] および [http://www.lua.org/wshop12/Ierusalimschy.pdf Lua 開発者によるプレゼンテーション] には、詳しい解説と小技があります。

[[utf8 (日本語)|utf8]] ライブラリを使うと UTF-8 で符号化された Unicode テキストを操作できます (この関数で指定された text 引数など)。

Android と iOS のデフォルトにおいて textinput は無効です。有効にするには [[love.keyboard.setTextInput (日本語)|love.keyboard.setTextInput]] を呼び出します。

== 用例 ==
ユーザが書いたテキストの記録と表示をします。
<source lang="lua">
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
</source>

ユーザが書いたテキストの表示、およびバックスペースが押されたときにテキストを消去します。
<source lang="lua">
local utf8 = require("utf8")

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

    	-- バックスペースを押し下げたままにして love.keypressed イベントを何度も起動するにはキーリピートを有効にします。
    love.keyboard.setKeyRepeat(true)
end

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

function love.keypressed(key)
    if key == "backspace" then
        -- 文字列末尾にある UTF-8 文字のオフセット・バイトを取得します。
        local byteoffset = utf8.offset(text, -1)

        if byteoffset then
            -- 末尾の UTF-8 文字を削除します。
            -- string.sub は UTF-8 文字 ではなくバイトで操作を行うため 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 (日本語)]]
[[Category:Callbacks (日本語)]]
{{#set:Description=ユーザによりテキストが入力されたときに呼ばれます。}}
{{#set:Subcategory=Keyboard (日本語)}}

== そのほかの言語 ==
{{i18n (日本語)|love.textinput}}