Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
Select the text with a hotkey. (similar as select text with double click)

When I double-click on text with the mouse, the system automatically selects the text for me.

Is there a way to replace this behavior by pressing a hotkey?


I try

<p>Hello world! 123 abc ABC</p>
<script>
  const HOTKEY = "Control"

  let clientX, clientY
  document.addEventListener("mousemove", (e) => {
    clientX = e.clientX
    clientY = e.clientY
  })

  document.addEventListener("keydown", (keyboardEvent) => {
    if (keyboardEvent.key === HOTKEY) {
      const elem = document.elementFromPoint(clientX, clientY)
      console.log(elem)
      elem.dispatchEvent(new MouseEvent("dblclick"))
    }
  })
</script>

It doesn't work.

Expected: For example, If I move the mouse to the world and press the key Ctrl, it should select the text, like below.

enter image description here


Optional reading

In practice, I can't know the actual element.

That is, I can't know through querySelector.

The system generated the element dynamically. The only reliable is to double-click to get the select text.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This will select all text in the node, but atleast it works.

<p>Hello world! 123 abc ABC</p>
<script>
  const HOTKEY = "Control"

  let clientX, clientY
  document.addEventListener("mousemove", (e) => {
    clientX = e.clientX
    clientY = e.clientY
  })

  document.addEventListener("keydown", (keyboardEvent) => {
    if (keyboardEvent.key === HOTKEY) {
      const node= document.elementFromPoint(clientX, clientY)

      if (document.body.createTextRange) {
        const range = document.body.createTextRange();
        range.moveToElementText(node);
        range.select();
      } else if (window.getSelection) {
        const selection = window.getSelection();
        const range = document.createRange();
        range.selectNodeContents(node);
        selection.removeAllRanges();
        selection.addRange(range);
      } else {
        console.warn("Could not select text in node: Unsupported browser.");
      }
    }
  })
</script>

Full credit goes to here: Selecting text in an element (akin to highlighting with your mouse)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda