Jump to content

GUI - Is there an event called when the user presses Enter?


Recommended Posts

onBeforeKeyAddObservable doesn't pick up Enter presses, and onBlurAddObservable doesn't differentiate between the user pressing Enter and clicking outside of the TextInput. 

What I'm trying to do is have a chat box, so that the user can send the message simply by pressing Enter; but I can't find a way to determine when the user actually presses Enter.

Thanks

Link to post
Share on other sites

Answering my own question...

The solution I came up with was remove the event listeners from the InputText, and add a 'keydown' event listener to the canvas. Then in the canvas' keydown event, if the user presses enter, I process the chat input's current text and then clear it. Otherwise, I call the InputText.processKeyboard(e) method, with e being the KeyboardEvent passed via the onkeydown event, and let the InputText control process it from there.

 

private _chatInput : GUI.InputText;

...

document.getElementById('canvas').addEventListener('keydown', this._handleKeyDown.bind(this));

...

private _handleKeyDown(e : KeyboardEvent) : void {
    if (this._chatInput) {
        if (e.key === 'Enter') {
            this._chatInput.text = '';
        } else {
            this._chatInput.processKeyboard(e);
        }
    }
}

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...