0

I'd like my LWC Headless Quick Action to behave differently when the alt/option key is held down. But the platform abstracts away the browser events and calls the invoke function with no additional information included:

import { LightningElement, api } from "lwc";

export default class HeadlessSimple extends LightningElement {
  @api invoke() {
    console.log("Hi, I'm an action.");
  }
}

A KeyboardEvent or MouseEvent include an altKey property to signal that. What is the best way of obtaining that state for a Headless Quick Action? Does include an onkeypress listener work?

1 Answer 1

1

This rather verbose code works:

handleKeyDown;
handleKeyUp;
altKey = false;

connectedCallback() {
    this.handleKeyDown = (event) => {
        if (event.which === 18) this.altKey = true;
    };
    this.handleKeyUp = (event) => {
        if (event.which === 18) this.altKey = false;
    };
    window.addEventListener('keydown', this.handleKeyDown);
    window.addEventListener('keyup', this.handleKeyUp);
}

disconnectedCallback() {
    if (this.handleKeyDown) window.removeEventListener('keydown', this.handleKeyDown);
    if (this.handleKeyUp) window.removeEventListener('keyup', this.handleKeyUp);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .