Programmer_Mao
找到解决方法了,但我感觉是godot的bug。

由于spinbox根本就没有获得焦点所以就没法release_focus(通过SpinBox.has_focus()显示一直为false)
调查发现spinbox的LineEdit才获得了焦点,所以解决方法是获得LineEdit并释放它(具体见代码_get_line_edit()
方法)。
extends SpinBox
var _l:LineEdit
func _ready() -> void:
_get_line_edit()
func _get_line_edit():
for c in get_children():
if c is LineEdit:
_l = c
break
#判断获得了有效lineEdit
assert(is_instance_valid(_l))
func _input(event):
#内置输入函数 当有输入是引擎自动调用
release(event)
func release(event:InputEvent):
var result = false #定义是否需要释放的变量
if event.is_action_pressed("ui_accept"):
#如果发生"ui_accept"动作则释放焦点 默认为回车键
result = true
#如果事件类型为鼠标按钮
if event is InputEventMouseButton:
#判断鼠标点击位置是否在输入框内
if is_inside(event.position):
#确保在框内点击时 永远能够获得焦点
if event.button_index == BUTTON_LEFT and event.pressed:
_l.grab_focus() #抓取焦点
else:
#鼠标任意按钮在输入框外点击
result = true
if result:
#如果为true则是否焦点
_l.release_focus()
func is_inside(checkpos: Vector2):
#获取输入框全局Rect矩形位置
var global_rect:Rect2 = get_global_rect()
#判断鼠标点击点是否在输入框全局Rect内
return global_rect.has_point(checkpos)