How to click out of line edit?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jujumumu

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.

:bust_in_silhouette: Reply From: Zylann

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

See Control — Godot Engine (3.1) documentation in English

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?

jujumumu | 2019-08-21 15:20

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?

Zylann | 2019-08-21 17:43

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

jujumumu | 2019-08-22 00:09

:bust_in_silhouette: Reply From: csujin

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()

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

T3chn0 | 2021-05-31 00:59

:bust_in_silhouette: Reply From: Fenisto

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()
:bust_in_silhouette: Reply From: Picster

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