Topic was automatically imported from the old Question2Answer platform.
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.
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.
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
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