• Godot Help
  • GD3/4 Why event is InputEventKey does not work here?

Hello, This is the code that is used to delete an Item from inventory slot, but when I tried to do it with a key press (K) it does not work. How to fix this?

func _ready():
	var slots = get_node("Panel/ScrollContainer/GridContainer");
	for _i in range(MAX_SLOTS):
		var slot = ItemSlotClass.new();
		slot.gui_input.connect(self.slot_gui_input.bind(slot));

func slot_gui_input(event : InputEvent, slot : ItemSlotClass):
	#WORK FOR MOUSE
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_RIGHT:
			if slot.item:
				print("aaa")

	#BUT DOES NOT WORK WITH KEYBOARD
	if event is InputEventKey and event.pressed:
		if event.keycode == KEY_K:
			print("bbb")
			if slot.item:
				print("bbb")

I'm using keyboard to move and when Item is in focused I pressed K but It does not print "bbb" , also no errors

Gowydot Something is strange... compare these pieces of code:
if event is InputEventMouseButton:
if event is InputEventKey and event.pressed:

Sooo, why add event.pressed:?

ThinKing_2005 Oh I just omitted out. This is original code:

if event.button_index == MOUSE_BUTTON_RIGHT && !event.pressed:

I got confused 'cause the Doc says gui_input covers InputEvent > InputEventAction which "Actions can be created from the Input Map" So I did that but also not working. I also tried with InputEventJoypadButton and also nothing...so just using mouse for now.

Anyway, I found the working remove item code and been trying to add ConfirmationDialog to it. Gonna need to open another question again soon 😕

    Gowydot !event.pressed This is probably the problem, because it basically means "the key is NOT pressed". (! is short for not). The code will obviously not work, because the !event.pressed is true when a certain key is pressed. Try simply removing !event.pressed altogether, it might actually work!

    Edit:

    Gowydot I got confused 'cause the Doc says gui_input covers InputEvent > InputEventAction which "Actions can be created from the Input Map" So I did that but also not working. I also tried with InputEventJoypadButton and also nothing...so just using mouse for now.

    I'm pretty sure this has nothing to do with the problem, because the mouse is working alright.

      ThinKing_2005 Yes I've already tried that. Also tried deleting InputEventMouseButton code, and just used InputEventKey(event.keycode / is_action_pressed) or InputEventJoypadButton (event.button_index) and still doesn't work.
      So gui_input() seems to only works for mouse like the guy in Github link said.

        ThinKing_2005
        Tried this
        slot.gui_input.connect(self.slot_gui_input.bind(slot))
        I got error says invalid get _input on base Panel. I think because the slot extends panel and can only use gui_input. No worries I'll just leave it as I don't wanna stray too far of original code or I'll get confused LOL.