The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

When I press the line edit I want it to be able to type but when I click outside of it it should not show it typing. How can I do that? I have tried setting the focus mode to different settings but it has not worked.

in Engine by (455 points)

4 Answers

0 votes

The LineEdit remains in the "typing mode" because it still has keyboard focus. To remove keyboard focus, you have to focus another control.

See https://docs.godotengine.org/en/3.1/classes/class_control.html#description

by (29,360 points)

So I have to add a script that detects when I click out of the LineEdit and gives the focus to another control. Is there not a way to do that without code like in the inspector?

When you "click outside of it", what are you actually clicking?
If it's a control, maybe change its focus mode. Otherwise, I don't know of a clean solution right now. I heard calling grab_focus() on a dummy control could help remove focus. Or, as the doc says, try to hide your line edit and show it again?

hiding it then reappearing removes the focus. Godot should have a remove focus function.

+1 vote

I find a way to solve the problem

attach a script with the code here:

extends LineEdit
func _is_pos_in(checkpos:Vector2):
var gr=get_global_rect()
return checkpos.x>=gr.position.x and checkpos.y>=gr.position.y and 
checkpos.x<gr.end.x and checkpos.y<gr.end.y

func _input(event):
if event is InputEventMouseButton and not _is_pos_in(event.position):
    release_focus()

and it will works

if you want to release focus also by keyboard like enter and etc.
you can change the code like this:

extends LineEdit
func _is_pos_in(checkpos:Vector2):
var gr=get_global_rect()
return checkpos.x>=gr.position.x and checkpos.y>=gr.position.y and 
checkpos.x<gr.end.x and checkpos.y<gr.end.y
func _input(event):
if event is InputEventMouseButton and not _is_pos_in(event.position) or 
event.is_action_pressed("ui_accept"):
    release_focus()
by (16 points)

Excellent solution just One question....

how do i attach this to multiple lineEdit at once and not one by one creating a script

EDITED

i put.. lets says a reference to the same script and works... BUT
simplier solution without scripts...

put in the parent node... supose a Control node search for focus in inspector an then change mode from None to ALL

0 votes

If there is a control node behind, like a background you can set it to send a signal when you click on the background to execute release_focus()

func _on_ColorRect_gui_input(event):
    if event is InputEventMouseButton:
        if event.pressed == true:
            $LineEdit.release_focus()
by (88 points)
+2 votes

I just read the answer below and while the approach is good, there is a cheaper and shorter way to get the same result:

extends LineEdit

func _input(event:InputEvent):
    if (has_focus()
    and event is InputEventMouseButton
    and not get_global_rect().has_point(event.position)):
        release_focus()

Just a bit shorter and makes use of the has_point() function of Rect2

by (64 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.