Mobile input enter error

hello I just started playingcanvas.

While designing multiplayer, I ran into an error in the chat-related function that the enter key was input on the PC but not on the mobile.

GamePlayManagerScript.prototype.update = function(e) {
    
    if(this.gameStarted === true)
    {
        if (this.app.keyboard.wasPressed(pc.KEY_ENTER))
        // if (this.app.keyboard.wasPressed(e.keycode === 13))
        {
            console.log("key : enter!");
            alert("input enter");
            // console.log(this.inputBoxChatMsgComp.script.input.checkFocus());
            
            if(this.inputBoxChatMsgComp.script.input.isFocued() === true)
            {
                var textBoxVal = this.inputBoxChatMsgComp.script.input.getCleanValue();
                
                this.lastMsg = textBoxVal;
                this.inputBoxChatMsgComp.script.input.deleteValue();
                this.inputBoxChatMsgComp.script.input.blur();
                
                if(this.lastMsg !== "")
                {
                    this.networkEntity.script.network.sendMsg(this.nicknameVal,this.lastMsg);
                }
            }
            else
            {
                this.inputBoxChatMsgComp.script.input.focus();
            }
            
            // console.log("this.lastMsg = ", this.lastMsg);
        }
        
        if(this.inputBoxChatMsgComp.script.input.isFocued() === true)
        {
            this.allowMovement = false;
        }
        else
        {
            this.allowMovement = true;
        }
    }
};

pc.KEY_ENTER and e.keyCode === 13 Both do not work, is there a problem with the script?

If not, thanks for letting me know what the problem is.

Hi @purpleline and welcome,

On mobile unless you have a keyboard attached to your phone, you can’t receive keyboard input events.

The on screen virtual keyboard doesn’t register like that. You will have to use other means of input registering (eg touch gestures) or UI on mobile.

2 Likes

Hello Leonidas

You have made the answer to the question clear.
So, I am thinking of changing the method and sending it using the button added during chatting.

However, sometimes the enter key is inputted.
If so, could it be that another script or screen composition is interfering?

Prevents the enter key on mobile keyboard from being used sometimes?

I am making modifications based on this project.
https://playcanvas.com/project/716927/overview/multiplayer-nickname-and-chatbox

Hmm, so I think if you have an HTML input field open, keydown events can fire on that.

But I’m not sure if app.keyboard will work.

Testing your project link now, if I type something on the input field and press enter on desktop doesn’t work either. Or am I doing something wrong?

After the first scene, enter is being input only in the chat window.

In PC, enter input works well, but on mobile, except for enter, it was confirmed that input works well.
Other keys work fine, but why only enter…