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.
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:
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.
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.
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
...
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.