How to make cursor always stay on textarea even if user clicks anywhere else on the page?

Issue

I’m developing a Chat application using Angular & Electron.

I want user’s cursor always on textarea where user types the message.
When user clicks anywhere else in the page, the cursor should not go away from text area. This helps user to type anytime from anywhere, it will type in textarea only.

I tried the following:

    setTimeout(() => {
      this.messageTextAreaInput.nativeElement.focus();
    }, 20);

I’m calling it on the textarea blur event. But this solution does not let me copy text from other messages. When I select any text from message history, my selection is lost.

Any other way to achieve this?

My app for reference:
enter image description here

Solution

window.addEventListener("keydown", () => {document.getElementById("textarea").focus()});
<textarea id="textarea" ></textarea>

Answered By – dangerousmanleesanghyeon

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.