1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Godot 4.2] LineEdit(とSpinBox)でEnterキーを押したら入力を終える

Last updated at Posted at 2023-12-19

line_edit.release_focus() を呼びましょう

Focus とは?

Focus とは、「アクティブなUI要素のうちどれが選択されているのか」を表す概念です。
「ゲームパッド操作でのメニュー操作中にカーソルが当たっているUI要素」をイメージすると分かりやすいかもしれません。
Focus を持つUI要素は必ず1つだけです。

LineEdit と Focus

テキスト入力ができるUI要素である LineEdit 、およびそれを利用して数値のみの入力ができる SpinBox(下図参照) では、「自身がフォーカスを持っているか否か」で「入力中か否か」を判定しています。

image.png

そして、デフォルトで Enter キーを押すと入力が確定される(かつ、対応したシグナルを発信する)ようになっているのですが……なんとフォーカスが外れません。
一応、「次の入力へ進む」だけならば Tab を押すと focus_next に設定されたノードへ進んでくれますが、「入力が終わったらとりあえずフォーカスを外したい」「Enter でも進んで欲しい」といった需要はあるかと思います。

Focus を外す方法

Control.release_focus() を呼ぶというわけです。
LineEdit は当然 Control を継承していますから、 line_edit.release_focus() のように書けば実装できます。

ただし、 SpinBox の場合は注意が必要です。
Focus を持っているのは SpinBox 本体ではなくて SpinBox.get_line_edit() で取得できる、内部の LineEdit の方です。
そのため、 spin_box.get_line_edit().release_focus() としないと何も起きません。

参考文献

Keyboard/Controller Navigation and Focus — Godot Engine (4.x)の日本語のドキュメント
Control — Godot Engine (4.x)の日本語のドキュメント
LineEdit — Godot Engine (4.x)の日本語のドキュメント
SpinBox — Godot Engine (4.x)の日本語のドキュメント

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?