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

111
Vistas
Custom-named mouseEvent and pointerEvent does not read the real state of modifier keys when previously created and programmatically dispatched

Disclaimer: I have found a working solution, this question is more of a RFC from a frontend newbie to the more experienced folks to see if there may a better solution and/or issues with the workaround I've come up with. According to etiquette in meta.stackexchange it is not perfect but permissible.

Background: I am making a custom element, and have set custom-named events for attaching particular event listeners. I am triggering those events upon registering click's on specific elements in the shadow DOM. The original code looks like:

// {this} points to the custom element class-object
//custom-named event
    evtSelect = new PointerEvent('select',
    {
        detail: {},
        bubbles: true,
        cancelable: true,
        composed: true,
    })

//synthetic dispatch element method
    select(event){
        this.setAttribute("selected","")
        this.dispatchEvent(this.evtSelect)
    }

//primary event listener
        this.shadowRoot.querySelector(`#slot-id`).addEventListener('click', this.select.bind(this))

When I add an event listener on the custom element for select, the synthetic event dispatch is correctly detected, but upon querying select.ctrlKey within the callback, it returns false regardless of the actual state of the Control key being pressed. In fact, it seems to capture no state whatsoever (including other modifier keys or cursor coordinates, for example). While I can statically set the state of the ctrlKey property on creating the event, what I am looking for is a way to query the actual state upon dispatching.

Fumbling around I've managed to get a working solution by changing the code to this:

//custom-named stored event is discarded entirely in favor of the following

//synthetic dispatch element method
    select(event){
        this.setAttribute("selected","")
        //here I create the event dynamically and anonymously, passing the originally triggering 'click' event as the source object to 'inherit' its state.
        this.dispatchEvent(new PointerEvent('select', event))
    }

//primary event listener
        this.shadowRoot.querySelector(`#slot-id`).addEventListener('click', (event)=>{this.select(event)})

My main concern with the working solution is that I may be inadvertendly creating a memory leak, as I'm unsure to what would be the scope of dynamically creating and dispatching an event in one go or where/if it may be latching on to some other scope, thus making itself reachable and not garbage-collectable, scaling up to a custom element that by function will be instanced many times. Second to that, I've found no hint in my searches of anyone facing this issue or any tutorial regarding this use-case, so I'm listening if there is a proposal for a more elegant solution.

about 4 years ago · Santiago Trujillo
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