Item alway returning to slot when touching inventory

Godot Version

V4.2.1

Question

when the mouse is not touching the inventory i made it to return to slot, but it always return to slot when i am touching the inventory. here the script:

extends Control

@onready var inventory: Inventory = preload("res://inventory/playerinventory.tres")
@onready var ItemStackGuiClass = preload("res://Gui/itemstackGui.tscn")
@onready var slots: Array = $NinePatchRect/GridContainer.get_children()
signal opened
signal closed
var mouse = false
var is_open: bool = false
var itemInHand: ItemStackGui
var oldIndex: int = -1
var slotclick = null

func update():
	for i in range(min(inventory.slots.size(), slots.size())):
		var inventorySlot: InventorySlot = inventory.slots[i]
		
		if !inventorySlot.item: continue
		
		var itemStackGui: ItemStackGui = slots[i].itemStackGui
		if !itemStackGui:
			itemStackGui = ItemStackGuiClass.instantiate()
			slots[i].insert(itemStackGui)
			
		itemStackGui.inventorySlot = inventorySlot
		itemStackGui.update()

func open():
	visible = true
	is_open = true
	opened.emit()


func close():
	visible = false
	is_open = false
	closed.emit()

func _ready():
	connectSlots()
	inventory.updated.connect(update)
	close()
	update()

func connectSlots():
	for i in range(slots.size()):
		var slot = slots[i]
		slot.Index = i
		slotclick = slots[i]
		slotclick.Index = i
		var callable = Callable(onSlotClicked)
		callable = callable.bind(slot)
		slot.pressed.connect(callable)

func  _input(event):
	if itemInHand && Input.is_action_just_pressed("rc") && mouse:
		print(mouse)
		putItemBack()
	updateItemInHand()
	if event.is_action_pressed("f"):
		if is_open:
			close()
		else:
			open()

func onSlotClicked(slot):
	if slot.isEmpty():
		if !itemInHand: return
		insertItemInSlot(slot)
		return
	if !itemInHand:
		takeItemFromSlot(slot)
		return
		
	if slot.itemStackGui.inventorySlot.item.name == itemInHand.inventorySlot.item.name:
		stackItems(slot)
		return
	swapItems(slot)

func takeItemFromSlot(slot):
	itemInHand = slot.takeItem()
	add_child(itemInHand)
	updateItemInHand()
	
	oldIndex = slot.Index

func insertItemInSlot(slot):
	var item = itemInHand
	
	remove_child(itemInHand)
	itemInHand = null
	
	slot.insert(item)
	
	oldIndex = -1
func swapItems(slot):
	var tempItem = slot.takeItem()
	
	insertItemInSlot(slot)
	
	itemInHand = tempItem
	add_child(itemInHand)
	updateItemInHand()
func updateItemInHand():
	if !itemInHand: return
	itemInHand.global_position = get_global_mouse_position() - itemInHand.size / 2


func stackItems(slot):
	var SlotItem: ItemStackGui = slot.itemStackGui
	var maxAmount = SlotItem.inventorySlot.item.maxamount
	var totalAmount = SlotItem.inventorySlot.amount + itemInHand.inventorySlot.amount
	
	if SlotItem.inventorySlot.amount == maxAmount:
		swapItems(slot)
		return
	
	if totalAmount <= maxAmount:
		SlotItem.inventorySlot.amount = totalAmount
		remove_child(itemInHand)
		itemInHand = null
		oldIndex = -1
	else:
		SlotItem.inventorySlot.amount = maxAmount
		itemInHand.inventorySlot.amount = totalAmount - maxAmount
		
	SlotItem.update()
	if itemInHand: itemInHand.update()



func putItemBack():
	if oldIndex < 0:
		var emptySlots = slots.filter(func (s): return s.isEmpty)
		if emptySlots.is_empty(): return
		
		oldIndex = emptySlots[0].index
		
	var targetSlot = slots[oldIndex]
	insertItemInSlot(targetSlot)
	







func _on_mouse_exited():
	mouse = true


func _on_mouse_entered():
	mouse = false

here the code for the code if mouse is touching inventory or not:

func _on_mouse_exited():
	mouse = true


func _on_mouse_entered():
	mouse = false

here where the signal connect: