Keyboard input while using Chiense input method seems remaining in the Input poll after submission

Godot Version

4.2.2 & 4.3

Question

I’m trying to add a chat system so that i can chat with my NPC with some AI models, say ChatGPT, with Chinese. So I made a little demo that my PC uses WASD to move, F to talk to the NPC. While talking, a chat interface pop out with a LineEdit and a button to exit. Just like this:

I can type some sentence with Chinese input method, and use Enter to submit the text. All works fine. However, when I click the exit button to exit the chat system, the PC will move along the direction as if my typing in the chat system is not cleared. For example, if i typed “aaaa” (actually in chincese input method, and submitted), the PC will keep moving to the left, as if i’m pressing key “A”.

I tried to debug with the following method, and observed that after exiting the chat system, without pressing any key, Input.is_anything_pressed() is true.

func _on_debug_timeout() -> void:
	print(Input.is_anything_pressed())
	print(str(direction) + " " + str(velocity))

Following is my _physics_process

func _physics_process(delta: float) -> void:
	
	if dialog_manager.canvas_layer.visible:
		direction = Vector2(0, 0)
	else:
		direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
		
	if direction.x > 0:
		animated_sprite.flip_h = false
	elif direction.x < 0:
		animated_sprite.flip_h = true
	
	if direction.is_zero_approx():
		animated_sprite.play("idle")
	else:
		animated_sprite.play("run")
		
	velocity = direction.normalized() * SPEED

	move_and_slide()

This could be true for keyboard joypad or action. Do you know if the key is sticking or the action?

Do you by chance inject any input/action into the Input system?

Also do you use Viewport.set_input_as_handled() when using the chat?

I also used an _input() function to check if any keys are pressed, but while the PC automaticlly moving (with Input.is_anything_pressed() being true), no button pressing is detected. What’s why i suspect that the cause may be the typing while using input method is not handled correctly.

This is an example while using Chinese input method:

The user input is some kinda of cached by the input method until the user chooses the wanted character.

Also, i didn’t use set_input_as_handled() in the chat system.

It could be a real bug.

Is Godot going to the background when you
use the chat interface?

Typically when an app doesn’t have focus it shouldn’t have access to the keyboard.

Does this happen only with chat app? Or other apps as well? If it’s only the chat app it could be a bug in the chat app injecting keystrokes into the system.

What is your operating system?

Is Godot going to the background when you use the chat interface? → I dont think so, as can be seen in the image, the chat interface is inside the Godot window. But i think, maybe, Godot goes to the background when i typing Chinese. It seems that the input method has focused when typing since it’s floating above the godot window.

Does this happen only with chat app? Or other apps as well? → I don’t think there is any chat app in my case. If you mean the Chinese input app, I tried two different chincese input methods, and they all have the same problem.

I’m using MacOS.

If you use a text editor and then use the input app. Does text show up on the text editor?

Yes. I think the input app works just fine in other apps.

My last suggestion, when inputting into the chat, would be to use.

get_viewport().set_input_as_handled()

And your character needs to use the _unhandled_input function, instead of a direct call to Input.get_vector() in the physics function. You can use get_vector after checking if the event is a move action that wasn’t already handled by the chat input.

# chat app
func _input(event):
   if chat_is_active:
     get_viewport().set_input_as_handled()
     ...
var direction = Vector2.ZERO
func _unhandled_input(event):
  if event.is_action("left") or event.is_action(...
    direction = Input.get_vector(...)
  if event.is_action("chat"):
    direction = Vector2.ZERO
  ...

But it also seems like this should work to. Is it really working?

Not really. It seems that my keyboard input while using the input method is cached somewhere. Though I set the direction to (0, 0) when I quit the dialog mode, the cached inputs are then handled and drives the chatacter to move. :frowning_face:

I will try this out later. Thanks a lot! :beers: