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